9tbqqw
Last Updated: February 25, 2016
·
386
· wkjagt
Ccc9abdf9963f3452808cff0e77679a1

A safe constructor in an extending class

Sometimes when you extend a class that's not yours, in a web framework for example, you need to have a constructor for your own class. You don't want to just put a constructor in there, because if the parent class has a constructor, it will be overridden. You could call parent::__construct() with the right arguments, but in a newer version of the the parent class, the signature of the constructor may change, and you would have broken it.

Python has *args and **kwargs for this, but in PHP the closest I have found is this:

call_user_func_array(array('parent', '__construct'), func_get_args());
Say Thanks
Respond