David Gard
David Gard

Reputation: 12087

Tell a PHP Class to always run it's constructor when it is extended

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.

Upvotes: 5

Views: 1321

Answers (5)

Hugo Delsing
Hugo Delsing

Reputation: 14173

You must always use parent::__construct() if you are createing a __construct in both classes You do have an option to turn it around. You could have your parent class constructor check for the existence of a custom constructor in the child and run it.

<?php
class Options_Page_Template{
    function __construct(){ 
        if (method_exists($this,'__beforeconstruct'))
          $this->__beforeconstruct();

        echo "construct class<br/>";

        if (method_exists($this,'__afterconstruct'))
          $this->__afterconstruct();

    }

    function on_enqueue_scripts(){
    }
}

class Options_Home extends Options_Page_Template{
    function __beforeconstruct(){ 
       echo "Extended class before<br/>";
    }

    function __afterconstruct(){ 
       echo "Extended class after<br/>";
    }
}

new Options_Home();

?>

output:

Extended class before
construct class
Extended class after

Upvotes: 4

Ed Heal
Ed Heal

Reputation: 60037

class Options_Home extends Options_Page_Template{

    function __construct() { 
       parent::__construct(); // Needs to be first line so the rest of the constructor
                              // can assume the Options_Page_Template has been constructed.
       // Various stuff here for Options_home constructor

    }


}

Should work.

Upvotes: 0

Jason
Jason

Reputation: 507

You could do something nasty like define your constructor as final and have a protected init method which extended classes can override but I wouldn't recommend it to be honest.

This way if someone tries to write a constructor in a child class they'll get something like:

PHP Fatal error: Cannot override final method A::__construct() in php shell code on line 5

You probably need to look at a different approach to the way you've organised your classes instead.

Upvotes: 2

Evert
Evert

Reputation: 99816

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.

Upvotes: 5

scottlimmer
scottlimmer

Reputation: 2278

Just don't define a constructor the for the extending class.

Upvotes: -2

Related Questions