Last Updated: January 28, 2019
·
576
· wkjagt

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());