Pale Purple https://www.palepurple.co.uk Office Address Registered Office Blount House, Hall Court, Hall Park Way,,
Telford, Shropshire, TF3 4NQ GB
sales@palepurple.co.uk GB 884 6231 01
PHP 5.6 is almost here (we hope) – so we’ve had a look at some of the upcoming changes, and here are the ones of most interest to us. In a nutshell – variadics & splat, constant scalar expressions and ArrayOf type hinting ….
The code in the examples below was tested against a locally compiled binary from the PHP 5.6 Git branch on 20th Jan 2014 (compilation was an uneventful ./configure && make – so it should be easy enough for anyone to test out the new features).
See also https://wiki.php.net/rfc/variadics
Aim – to allow functions to have variable arguments, and show this within the method signature.
PHP has always allowed extra (not defined) parameters to be passed to a function. To access these you’d have needed to call ‘func_get_args()’.
Now we can define a …$variable which will catch any extra parameters within the function signature.
So, for example, the following code :
function myFunction($parameter) { $otherArguments = array_slice(func_get_args(), 1); }
Could be called like : myFunction("Hello", "World", false)
(leaving $otherArguments to contain [“World”, false]).
With Variadic support, it could be written as :
function myFunction($parameter, ...$otherArguments) { // No need for func_get_args() }
Variadic parameters need to be a function’s last specified parameter.
If required, variadic parameters can be passed by reference ( &...$otherArguments
) and can have type hinting applied to them (i.e. every additional argument has to conform to the type hint).
See also https://wiki.php.net/rfc/argument_unpacking
Aim – to make it easier to call a function using the contents of an array as it’s parameters.
So, if you have a array containing things you wish to use for parameters to a function call, you’d previously have had to do :
call_user_func_array('myFunction', $myParameters);
Now, assuming $myParameters contains [‘Hello’, ‘World’] the following will be equivalent in PHP 5.6 –
As with variadics, the unpacking can be performed by reference ( &…$myParameters ).
Just remember the array ($myParameters) cannot currently contain string keys – clearly someone’s hoping that PHP will one day support named parameters like Python (doing this will trigger :
Catchable fatal error: Cannot unpack array with string keys in ......./test.php on line 9
at present.
See also : https://wiki.php.net/rfc/const_scalar_exprs
Currently const, class property and function argument declarations have to be constant (unchanging) values. This RFC allows the declaration to be more expressive i.e. supporting the ternary operator, concatenation and all the normal arithmetic and bitwise operators.
For example, before PHP 5.6 we could have code like :
class Example { const CONSTANT = 4; const DATE = 2014; static $staticProperty = false; protected $property = [1,2,3]; }
With PHP 5.6, we can also have (I’ve shown as properties, but the same applies to consts or statics):
class Example { // in addition to the above ... // concat. strings: protected $a = "Hello" . "world"; protected $b = __DIR__ . '/something.php'; // some maths: protected $c = 1 + 1; protected $d = self::CONSTANT + 2; protected $e = 1024 * 1024 * 2; // ternary operator ( x ? y : z ) : protected $f = 2014 < self::DATE ? true: false; }
See also the RFC for ArrayOf
In a nutshell – this would allow for the enforcement of a specific object type within an array.
This seems to be quite a new RFC and hasn’t yet been merged into the 5.6 branch. Hopefully it will be included.
e.g.
Rather than :
function test(array $list) { foreach($list as $item) { if(! $item instanceof Something) { throw new RuntimeException('List item not a Something'); } } }
You could have :
function test(Something[] $list) { // everything in $a is an instanceof Something (And also not null). }
So cutting down on some boilerplate code and so on – and improving the documentation of functions in the same way as the variadic support above.
‹ FourSquare API Integration thoughts Porting a small screen Android watch app to a ‘normal’ smartphone size ›
RT @PalePurpleLtd: Looking ahead to PHP 5.6: PHP 5.6 is almost here (we hope) – so we’ve had a look at some of the new bits. http://t.co/aK…
RT @PalePurpleLtd: Looking ahead to PHP 5.6: PHP 5.6 is almost here (we hope) – so we’ve had a look at some of the new bits. http://t.co/aK…
[…] details available on Looking ahead to PHP 5.6 and New Features in PHP […]