Last Updated: January 28, 2019
·
1.04K
· 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());

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#php
#oop
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside