PHP 5.5 New Features PRO

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.

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

image of woman and database

Record Classes in Java

Learn how to define record classes in Java, and when to use record classes in your code. Stay up to date with the new Java features.

Accessibility | Career | Conferences | Design Patterns | JavaScript | Meetups | PHP | Podcasts | Python | Self Learning

Teaching Methodologies | Fullstack | C++ | C# | CSS | Node.js | Angular | Java | Go | Android | Kotlin | Swift | Academy

Front End Development | Scala | Architectures | Cloud | Big Data | Internet of Things | Kids Learn Programming

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Skip to content Update cookies preferences