From what I understand, when a PHP Class is extended, it's constructor is not called? So in the below, the method Options_Page_Template->on_enqueue_scripts()
is never actually run -
class Options_Page_Template{
function __construct(){
/** Enqueue scrips and styles for use in this plugin */
add_action('wp_enqueue_scripts', array(&$this, 'on_enqueue_scripts'));
}
function on_enqueue_scripts(){
{ do Whatever here }
}
}
class Options_Home extends Options_Page_Template{
function __construct(){
{ Add various actions here }
}
}
Is there a way of telling Options_Page_Template()
to always run it's own __constructor()
when it is extended? I know I can use parent::__construct()
, but that involves adding the code to all the files that extend the Class.
I also tried the the old-school method of giving the constructor the same name as the Class, but PHP is obviously more clever than that, as it did not work. Thanks.
There's no way. A good rule of thumb for all your code, is to always call parent::__construct for every subclass that overrides the constructor; and explicitly comment within the constructor if you are not, and why.
In general, for functions and con/destructors, if you override something; you are also responsible for calling the parent function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With