Call parent constructor before child constructor in PHP

22,332

Solution 1

Just call parent::__construct in the child.

class Form extends Tag
{
    function __construct()
    {
        parent::__construct();
        // Called second.
    }
}

Solution 2

yeah just call parent::__construct() in your construct

Solution 3

Yes, but only internally (i.e., by writing a PHP extension), so if I were you I'd settle with calling parent::__construct(). See this section on the PHP wiki.

Sorry, PHP is not Java. I think not requiring (implicitly or explictly) the super constructor to be called was a very poor design decision.

Solution 4

From the sounds of it you may want to rethink your design so that you don't need to pass the parameters in the constructor. If you don't think it can be done, ask it as a question, you might be surprised by some of the suggestions.

The child class has the ability to override the parent constructor without calling it at all. I would recommend having a final method in the parent class. That way everyone knows you don't want this being overriden, and any inherited class (rightly) has access to do whatever it wants in the constructor.

class Father {
    private $_privateData;
    final function setPrivateData($privateData) {
        $this->_privateData = $privateData;
    }
}

Another, not recommended, more "reinventing the wheel", solution would be to define a function in the parent class, say _construct(), that's called in its own construct. Its not really clear, doesn't use language features/constructs, and is very specific to a single application.

One last thing to keep in mind: you can't really hide information from the child class. With Reflection, serialize, var_dump, var_export and all these other convenient APIs in the php language, if there is code that shouldn't do anything with the data, then there's not really much you can do asides from not store it. There are libraries and such that help create sandboxes, but its hard to sandbox an object from itself.

Edit: Somehow I missed Artefacto's answer, and I suppose he is right (I've never tried writing an extension to do that). Still, implementing it breaks developer expectations while making it harder to actually see code to explain what's going in.

Share:
22,332
Matt
Author by

Matt

Updated on June 21, 2020

Comments

  • Matt
    Matt almost 4 years

    I was wondering if its possible to call the parents __construct(), before the child's __construct() with inheritance in PHP.

    Example:

    class Tag {
         __construct() {
             // Called first.
         }
    }
    
    class Form extends Tag {
         __construct() {
             // Called second.
         }
    } 
    
    new Form();
    

    Ideally, I would be able to do something in between them. If this is not possible, is there an alternative, which would allow me to do this?

    The reason I want to do this is to be able to load a bunch of default settings specific to the Tag that Form can use when __construct() is called.

    EDIT: Sorry forgot to add this.. I'd rather not call the parent class from the child class. It's simply because it exposes some private data (for the parent) to the child, when you pass it as an argument

    This is what I want to do:

    $tag = new Tag($privateInfo, $publicInfo);
    $tag->extend(new Form()); // Ideal function, prob doesn't work with inheritance.
    

    Tag.php

    class Tag {
         private $privateInfo;
         public $publicInfo;
         __construct($private, $public) {
             $this->privateInfo = $private;
             $this->publicInfo = $public;
         }
    } 
    

    Form.php

    class Form extends Tag {
    
         __construct() {
             echo $this->publicInfo;
         }
    } 
    

    Make sense?

    Thanks! Matt Mueller