<?php
/* Function Argument Unpacking in PHP 5.6 complements Variadic Functions! */
class Foo {
public function __construct($arg1, $arg2, $arg3) {
/* Do stuff constructor with arguments */
$this->arg1 = $arg1;
$this->arg2 = $arg2;
$this->arg3 = $arg3;
}
}
class Bar extends Foo {
public function __construct(...$args) {
/**
* Before argument unpacking in 5.6 we would use
* call_user_func_array + func_get_args to accomplish
* the same thing.
*/
// The intent here is to call the parent constructor
parent::__construct(...$args); // Looks much nicer in 5.6
}
}
$bar = new Bar(1,2,3);
var_dump($bar);
Deprecated: Creation of dynamic property Bar::$arg1 is deprecated in /in/6ibqf on line 6
Deprecated: Creation of dynamic property Bar::$arg2 is deprecated in /in/6ibqf on line 7
Deprecated: Creation of dynamic property Bar::$arg3 is deprecated in /in/6ibqf on line 8
object(Bar)#1 (3) {
["arg1"]=>
int(1)
["arg2"]=>
int(2)
["arg3"]=>
int(3)
}
Parse error: syntax error, unexpected '.', expecting '&' or T_VARIABLE in /in/6ibqf on line 13
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '.', expecting '&' or T_VARIABLE in /in/6ibqf on line 13
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/6ibqf on line 4
Process exited with code 255.
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/6ibqf on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/6ibqf on line 4
Process exited with code 255.