Been using PHP forever, and found a condition like this while debugging ZF3 Factories.


class Foo {
public function __construct( $a, $b, $c ){
print_r( [$a, $b, $c] );
}
}
new Foo( 1, 2, 3, 4, 5, 6 );

What a strange thing. You can define a fixed constructor for an object, and overfeed the constructor during instantiation without hearing a word of it.
This works for any function, not just constructors, but your linter will scream at you for doing it — and for good reason. JS is worse: if you don’t pass enough args, it just fills them with undefined.
I knew of this specific to functions, but have always indexed constructors as such sacred creatures, that I never made the association!
TIL moment after nearly 20 years of use, noticed while auditing others’ code. Felt scandalized, thought I’d share!
This is documented in https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list :
“Note: It is also possible to achieve variable-length arguments by using func_num_args(), func_get_arg(), and func_get_args() functions. This technique is not recommended as it was used prior to the introduction of the … token.”
edit: also see https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list.old
This is useful when you have to maintain OS code and arguments evolves, that way you can see how many arguments have been passed (func_num_args()), adapt and be retrocompatible until your next major release
Members
Online

source