PHP 5.6 was released on August 28th 2014. This post overviews the main new features this new version introduces.

Arguments Unpacking

We can use the ‘…’ operator for passing over an array of values to a function we call and have the array values unpacked and assigned into the function parameters. The following code sample shows how to do it.

<?php
function total($a,$b,$c)
{
    return $a+$b+$c;
}

$vec = [12,4,54];

echo total(...$vec);
?>

The following video clip overviews this code sample, shows its execution and explains it step by step.

Constants Scala Expressions

We can create constants assigned with values of expressions that include the use other constants together with scalar values. The following code sample shows that.

<?php
const SUNDAY = 1;
const MONDAY = SUNDAY + 1;

class Something {
    const TUESDAY = MONDAY + 1;
    const FRIDAY = 2 * Something::TUESDAY;
    const STR = 'The value of FRIDAY is '.Something::FRIDAY;

    public function getSeventhDay($number = Something::FRIDAY + 1)
    {
        return $number;
    }
}

echo (new Something())->getSeventhDay();
?>

The following video clip overviews this code sample, shows its execution and explains it step by step.

The Default Charset Encoding

The php.ini configuration file includes the default_charset setting, that sets the default encoding charset been used in the dynamic web pages our PHP execution environment includes. The following code sample shows how we can easily get that value in our code.

<?php
echo ini_get("default_charset");
?>

The following video clip shows the execution of this code sample, overviews it and explains it step by step.

The Exponantiation Operator

The ** exponantiation operator allows us to calculate the exponantiation of a number we have by another. The following short code sample shows how to use it.

<?php
$number = 2;
$result = $number ** 3;
echo "\n".$result;
$num = 2;
$num **= 3; //$num = $num ** 3
$num **= 2; //$num = $num ** 2
echo "\n".$num;
?>

The following video clip overviews this code sample, shows its execution and explains it step by step.

The gost-crypto Hash Algorithm

The gost-crypto hash algorithm was added in PHP 5.6. We can use it for calculating the hash code for any data we hold. The simplest way for using this algorithm involves with calling the hash function. The following code sample shows how simple it is to use this new hash algorithm.

<?php
$vec = hash_algos();
var_dump($vec);

$password = "abcjojo123";
echo "\n".hash("gost-crypto",$password);
echo "\n".hash("gost-crypto",$password);
?>

The following video clip shows the execution of the above code sample, overviews its code and explains it step by step.

The __debugInfo debug Info

The __debugInfo new magic function allows us to set which properties will be presented together with their values when passing over an object to the var_dump() function. The following code sample shows how simple it is to use this new magic function

<?php
class Something
{
    private $name;

    public function __construct($str)
    {
        $this->name = $str;
    }

    public function __debugInfo()
    {
        return [
            'length of name' => strlen($this->name),
            'name starts with' => substr($this->name,0,1)
        ];
    }
}

$ob = new Something("james");
var_dump($ob);
?>

The following video clip shows the execution of this code sample, overviews it and explains it step by step.

<object width=”480″ height=”360″><param name=”movie” value=”//www.youtube.com/v/x1yfBD62Siw?version=3&amp;hl=en_US” /><param name=”allowFullScreen” value=”true” /><param name=”allowscriptaccess” value=”always” /><embed type=”application/x-shockwave-flash” width=”480″ height=”360″ src=”//www.youtube.com/v/x1yfBD62Siw?version=3&amp;hl=en_US” allowscriptaccess=”always” allowfullscreen=”true”></embed></object>

The use Operator

The use operator allows us to avoid writing the full qualifies names of classes that belong to other namespaces. As of PHP 5.6 we can use this operator to do the same with constants and global functions. The following code sample shows that.

<?php
namespace A\B {
    const NUMBER = 42;
    function doSomething()
    {
        return "hello"."\n";
    }
}

namespace MAIN
{
    use const A\B\NUMBER;
    use function A\B\doSomething;

    echo NUMBER."\n";
    echo doSomething();
}
?>

The following video clip overviews this code sample, shows its execution and explains it step by spte.

Variadic Functions

We can now easily define new functions with a varied number of parameters using the ‘…’ operator. The following code sample shows how to do it.

<?php
function func($required_parameter, $optional_parameter = 'x', ...$the_rest_of_the_parameters)
{
    // $params is an array that contains the rest of the arguments
    printf('$required_parameter: %s   $optional_parameter: %s   number of the rest of parameters: %d'."\n",
        $required_parameter, $optional_parameter, count($the_rest_of_the_parameters));
}

function sum(...$numbers)
{
    $total = 0;
    foreach($numbers as $number)
    {
        $total += $number;
    }
    return $total;
}

func('a');
func('a', 'b');
func('a', 'b', 'c');
func('a', 'b', 'c', 'd');
func('a', 'b', 'c', 'd', 'e', 'zzz');

echo "\n".sum();
echo "\n".sum(7);
echo "\n".sum(1,2);
echo "\n".sum(1,2,3,4,5,6,7);
?>

The following video clip overviews this code sample, shows its execution and explains it step by step.

You can find more training material and video clips for learning PHP in my free online courses at http://abelski.lifemichael.com. You can download the code samples shown in this post at php_5_6_samples.

Leave a Reply

Your email address will not be published. Required fields are marked *