PHP 5.5 adds many new improvements. Most of them don’t have any impact on code that was written in according with PHP 5.4.
We can now get the fully qualifies name of a class by appending its name with the ::class keyword. This new feature is especially relevant when using namespaces.
<?php namespace com\lifemichael\samples; class Rectangle{} echo Rectangle::class; ?>
The following video clip overviews this code sample, shows its execution and explains it.
The empty() function as of PHP5.5 can get an expression. It is no longer limited for getting a variable name. When passing over an expression it will return true if the expression evaluates to false. The following code sample shows that.
<?php function checknum($num) { if($num>0) return true; else return false; } if (empty(checknum(42))) { echo "42 "; } if (empty(checknum(-52))) { echo "-52 "; } if (empty(false)) { echo "false "; } if (empty(true)) { echo "true "; } ?>
The following video clip overviews this code sample, shows its execution and explains it.
As of PHP5.5 we can dereference a string directly after its creation within the very same statement. The following code sample shows that.
<?php echo "michael"[2]; ?>
The following video clip overviews this code sample, shows its execution and explains it.
As of PHP5.5 we can use the password_hash function for getting the hash value for the password the user entered. The following code sample shows how to use this function.
<?php $str = "haimmichael"; echo password_hash($str, PASSWORD_DEFAULT)."\n"; ?>
The following video clip overviews this code sample, shows its execution and explains it.
As of PHP5.5 we can add right after the last catch block a finally block. The finally block always executes. Whether an exception took place or not. Whether the exception was handheld or not. The finally block always executes. The following code sample shows that.
<?php function divide($a,$b) { if ($b===0) { throw new Exception('cannot divide by zero!'); } return $a/$b; } try { echo divide(5,2)." "; } catch (Exception $e) { echo 'exception:\'', $e->getMessage(), "' "; } finally { echo "finally1! "; } try { echo divide(4,0)." "; } catch (Exception $e) { echo 'exception:\'', $e->getMessage(), "' "; } finally { echo "finally2! "; } ?>
The following video clip overviews this code sample, shows its execution and explains it step by step.
As of PHP5.5 we can define a generator function. The object a generator function returns its reference can be iterated using a simple foreach loop. The following code sample shows that.
<?php function powpow($vector) { foreach($vector as $v) { yield $v*$v; } } $vec = [1,2,3,4,5]; $otherVec = powpow($vec); foreach ($otherVec as $number) { echo "$number "; } ?>
The following video clip shows the execution of this code sample, overviews it and explains it step by step.
You can find more video clips and training material for learning the PHP5.5 new features in my PHP Fundamentals course. You can find its free version at http://abelski.lifemichael.com.