22

When getting inherited from a parent class in PHP, especially in Codeigniter what does parent::__construct or parent::model() do?

How would it make difference if I don't __construct parent class? And, which way is suggested?

-Added-

The focus is more on Codeigniter specific regarding a call to parent::__construct in different ways depending on versions and also if this could be omitted in case Codeigniter would do this automatically.

Abdul Manan
  • 2,255
  • 3
  • 27
  • 51
Seong Lee
  • 10,314
  • 25
  • 68
  • 106
  • 3
    learn about `constructors`. – Nil'z Jul 23 '13 at 11:56
  • parent::__construct() SubClass constructor [Doc](http://php.net/manual/fr/language.oop5.decon.php) – Bora Jul 23 '13 at 11:57
  • well, I know what constructors do. They are one of those magic methods that will automatically run when class is initiated but what I am asking is the difference it would make if I don't declare this as the outcome seems to be not much different especially with Codeigniter. – Seong Lee Jul 23 '13 at 11:59
  • 3
    @SeongLee: What Niloy means is that, in PHP, if a child has a constructor, the parent constructor won't be called, unless you explicitly call it using `parent::__construct();`. in other words: the child _masks_ the parent constructor method, so you need to specify the `parent::` scope explicitly. So the answer to your question is: _"That depends on what the parent constructor does, and if you need it"_ – Elias Van Ootegem Jul 23 '13 at 12:01
  • @Elias Van Ootegem Thanks. That answers my question. – Seong Lee Jul 23 '13 at 12:04
  • 3
    Specifically in CodeIgniter, If you don't call `parent::__construct();` when the application controller is initializing, you'll lose `Loader and Core` class and `$this->load` would never works. check [`system/core/Controller.php`lines: 37-54](https://github.com/EllisLab/CodeIgniter/blob/2.1-stable/system/core/Controller.php#L37-L54). – Hashem Qolami Jul 23 '13 at 12:08
  • @HashemQolami Thanks for Codeigniter specific suggestion. Do you know which declaration is more suggested between parent::__construct and parent::parentmodel()? – Seong Lee Jul 23 '13 at 12:20
  • @SeongLee [`parent::Model();`](http://www.8tiny.com/source/codeigniter/system/libraries/Model.php.html) belongs to old versions of CodeIgniter _(v1.x)_ which had been used in models _(there was `parent::Controller();` for controllers as well)_. Since CI v2.x , `__construct();` magic method is used for the class initializing. However using `parent::__construct();` in models doesn't do anything special _(just logs a debug message)_ and it is not needed at all. – Hashem Qolami Jul 23 '13 at 13:44
  • Hi @HashemQolami it seems that you are well on point with my question! So, you are suggesting to use parent::__construct(); with controllers only? – Seong Lee Jul 23 '13 at 21:52
  • 1
    @SeongLee Using `parent::__construct();` is needed only if you want to declare `__construct()` method in your Controller which it will override the parent's one. That's true for models as well, but using `parent::__construct();` in model just logs a debug message `"Model Class Initialized"`, So if you need to know when Model is initialized _(in logs)_, keep using that, If not, ignore it. – Hashem Qolami Jul 24 '13 at 07:45

3 Answers3

89

This is a normal class constructor. Let's look at the following example:

class A {
    protected $some_var;

    function __construct() {
        $this->some_var = 'value added in class A';
    }

    function echo_some_var() {
        echo $this->some_var;
    }
}

class B extends A {
    function __construct() {
        $this->some_var = 'value added in class B';
    }
}

$a = new A;
$a->echo_some_var(); // will print out 'value added in class A'
$b = new B;
$b->echo_some_var(); // will print out 'value added in class B'

As you see, class B inherits all values and functions from A. So the class member $some_var is accessible from A as well as from B. Because we've added a constructor in class B, the constructor of class A will NOT be used when you are creating a new object of class B.

Now look at the following examples:

class C extends A {
    // empty
}
$c = new C;
$c->echo_some_var(); // will print out 'value added in class A'

As you can see, because we have not declared a constructor, the constructor of class A is used implicitly. But we can also do the following, which is equivalent to class C:

class D extends A {
    function __construct() {
        parent::__construct();
    }
}
$d = new D;
$d->echo_some_var(); // will print out 'value added in class A'

So you only have to use the line parent::__construct(); when you want a constructor in the child class to do something, AND execute the parent constructor. Example given:

class E extends A {
    private $some_other_var;

    function __construct() {
        // first do something important
        $this->some_other_var = 'some other value';

        // then execute the parent constructor anyway
        parent::__construct();
    }
}

More information can be found here: http://php.net/manual/en/language.oop5.php

arserbin3
  • 6,010
  • 8
  • 36
  • 52
giorgio
  • 10,111
  • 2
  • 28
  • 41
  • 2
    @webber .. Sorry to reject your edit. It was indeed correct, so I've pushed it through. Good catch on the `private` bug – arserbin3 May 20 '14 at 00:27
2

what does parent::__construct or parent::model() do?

these functions do exactly the same, only the construct function used to be named after the class itself prior to PHP5. I say in your example you are extending the Model class (and on some older version of CI since you don't need to use CI_model), if I'm correct in this __construct is the same as model().

2

It will simply execute the constructor of parent class

Justin Herrera
  • 526
  • 4
  • 11