Category : Just for Fun
Category : Weekly Test
What does the following code echo out:
|
1 2 3 4 5 |
if('ORANGE' > 'apple') {
echo 'orange';
} else {
echo 'apple';
} |
PHP 5.4 is just around the corner, and with it several new features and enhancements. Here are just a few of the features I’ve had the chance to play with that may be useful.
Arrays
Finally, in PHP you can now use the short bracket syntax for arrays. While really not a huge deal, this aligns PHP with many of the other programming languages, and will probably make switching from another language just a little easier (although array(1,2,3) is pretty simple).
|
1 2 3 4 5 |
$oldArray = array(1,2,3);
echo (string) $oldArray[1]; // 2
$newArray = [1,2,3];
echo (string) $newArray[1]; // 2 |
Another nice feature in regards to arrays is Array Dereferencing, or being able to access an array object directly off of a method or function.
|
1 2 3 4 5 6 7 8 9 10 11 |
function getArray()
{
return array(1,2,3);
// or return [1,2,3]; in PHP 5.4 ;)
}
// PHP 5.3
echo (string) getArray()[1]; // error
// PHP 5.4
echo (string) getArray()[1]; // 2 |
Class Instantiation Chaining
Also in PHP 5.4, class instantiation chaining was added, allowing you to chain off of the instantiation of a class by using paranthesis.
|
1 2 3 4 5 6 7 8 9 10 11 |
class Test
{
public function hello()
{
echo 'hello world';
}
}
// in PHP 5.3 this would be an error
// in PHP 5.4 it echos 'hello world'
echo (new Test())->hello(); |
Traits
Ok, this is a super simple overview of traits, but the addition of traits provides the flexibility of “extending” multiple classes without having to go through an entire chain. Unlike many languages, PHP Traits can also contain properties, however, if the class contains a property with the same name it will throw an E_STRICT warning IF the class property === the trait property, or a fatal error if the class property !== the trait property.
To use a trait, you would write something like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
trait MyTrait()
{
public $MyProperty = 'hello';
public function MyMethod()
{
echo 'world';
}
}
class MyClass()
{
use MyTrait;
public function DoStuff()
{
echo $this->MyProperty;
echo ' ';
$this->MyMethod();
}
}
(new MyClass())->DoStuff(); // echos out 'hello world' |
More to Come!
There’s a lot more features in PHP 5.4 that I still need to explore, including PHP Session Uploading, improved Closures, and a built in HTTP server. So stay tuned and I will try to give a quick overview of those as well as soon as I can get enough time to play with them
Category : Weekly Test
smrtClass is a restrictive data type class based on PHP’s stdClass. Instead of allowing the free creation and setting of properties as the stdClass does, smrtClass requires properties to be setup first with a specific type or regex requirement. Data is then checked against this requirement when the property is modified, and if inconsistent with the data requirements throws an exception.
smrtClass is designed to provide an added layer of security to PHP applications, and while it does not provide 100% protection, it does signigicantly help to prevent malicious, dangerous, or malformed data from making its way into the applications crucial systems.
smrtClass also contains locking mechanisms to prevent data from being deliberately or accidentally modified.
CLASS METHODS
== smrtClass::add ==
smrtClass::add( $var_name, $var_type = ['string'|'int'|'bool'|'object'|'array',|'date'|'datetime'|'timestamp'|'email'|'url'], $value = null );
|
1 2 3 4 5 6 7 8 9 10 |
$test = new smrtClass();
$test->add('number', 'int');
$test->number = '10'; // converted to integer
echo gettype($test->number): // displays "integer"
$test->add('string');
$test->string = 'Hello World'; // set as a string
$test->add('emailAddress', 'email');
$test->emailAddress = 'test@test.com'; // validated to be an email and set as string |
== smrtClass::addRegex ==
smrtClass::addRegex( $var_name, $regex, $value = null );
|
1 2 3 4 5 6 7 8 |
$test = new smrtClass();
$test->addRegex('numberOnly', '/^[0-9]+$/');
$test->numberOnly = 10; // valid
$test->numberOnly = 'ten'; // throws exception
$test->add('alphaOnly', '/^[a-z]$/i');
$test->alphaOnly = 'Hello'; // valid
$test->alphaOnly = 'Hello world'; // throws an exception because of the space |
== smrtClass::set ==
smrtClass:set( $var_name, $value )
|
1 2 3 4 |
$test = new smrtClass();
$test->add('name');
$test->set('name', 'Michael');
$test->name = 'Michael'; // does the same thing as set |
== smrtClass::remove ==
smrtClass:remove( $var_name )
|
1 2 3 4 5 |
$test = new smrtClass();
$test->add('name');
$test->name = 'Michael';
$test->remove('name'); // removes the name property
unset($test->name); // does the same thing as remove |
== smrtClass::listTypes ==
smrtClass::listTypes()
returns an array of supported types such as string, int, bool, etc.
|
1 2 |
$test = new smrtClass();
$test->listTypes(); |
Installing smrtClass
smrtClass can be run on any server utilizing PHP 5.3
Coding Restrictions
- smrtClass is limited to the same property naming restrictions as stdClass
- smrtClass currently does not allow for data types to be changed
Bugs
- No known errors
Awards (Yay) - listed in the order received
We’re working on this… lol
Usage
smrtClass was released under the GPL version 2 license.
Download
Category : Weekly Test
What does the following code produce in PHP 5.0+
|
1 2 3 4 5 6 |
$var = (object) array('0'=>'hello world');
if(isset($var[0])) {
die('Is Set');
}
die('Not Set'); |
Category : Uncategorized
(February 24, 1955 – September 5, 2011)
Thank you for all you have done for technology over the years, for bringing us the Apple, the Macintosh Color Classic, the iMac, the iPod, the iPhone, and recently the iPad… your amazing innovative will be greatly missed. Rest in Peace.
Category : Just for Fun
Category : Weekly Test
In PHP empty(array()) returns true, where-as empty(array(1)) returns false. What does the following code produce in PHP 5.0+
|
1 2 3 4 5 6 |
$var = (object) array();
if(empty($var)) {
die('Is Empty');
} else {
die('Is Not Empty');
} |
Category : Weekly Test
Assuming $cache->load($key) contains a cache of “yes” and $mediaCacheEnabled = true, what will the var_dump of $data return in this case?
|
1 2 3 4 5 |
if (!$data = $cache->load($key) || !$mediaCacheEnabled) {
$data = 'no';
}
var_dump($data); |



