I have a class in PHP like so:
class ParentClass {
function __construct($arg) {
// Initialize a/some variable(s) based on $arg
}
}
Check out these functions on php.net:
func_get_args
func_num_args
Also, if you want to make an optional argument, you can do this:
class ParentClass {
function __construct($arg, $arg2="Default Value") {
// Initialize a/some variable(s) based on $arg
}
}
parent::__construct( func_get_args() );
Yeah, it's pretty bad practice to make a child class that uses different constructor arguments from the parent. Especially in a language like PHP where it's poorly supported.
Of course, the generic way to pass a set of "whatever arguments we might ever want" in PHP is to pass a single argument consisting of an array of configuration values.
if you get php nested limit error, try this:
$args = func_get_args();
call_user_func_array(array('parent', '__construct'), $args);
There is something like this in php, though a bit verbose:
$args = func_get_args();
call_user_func_array(array($this, 'parent::__construct'), $args);
This can be done in PHP >= 5.6 without call_user_func_array()
by using the ... (splat) operator:
public function __construct()
{
parent::__construct(...func_get_args());
}