Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why do we still need parent constructor when controller class extends a parent controller?

I'm a beginner in CodeIgniter and OOP. I was reading a page of CI tutorial here. I found something that made a question in my mind.
Look at this code:

<?php
class News extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('news_model');
    }

I think if we made a class that extends CI_Controller, we assume it must have all methods and properties in its parent class (Although we can override them). So, why there is parent::__construct(); in the code?

like image 587
Mohammad Saberi Avatar asked Feb 25 '13 09:02

Mohammad Saberi


People also ask

What is the use of parent __construct?

We can do this by using the special function call parent::__construct(). The "parent" part means "get the parent of this object, and use it", and the __construct() part means "call the construct function", of course. So the whole line means "get the parent of this object then call its constructor".

Why we use parent constructor in CodeIgniter?

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.

Do you have to call parent constructor?

Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.

Does child class call parent constructor?

If the child class constructor does not call super , the parent's constructor with no arguments will be implicitly called. If parent class implements a constructor with arguments and has no a constructor with no arguments, then the child constructors must explicitly call a parents constructor.


2 Answers

__construct() is the constructor method of a class. It runs if you declare a new object instance from it. However, if a class implemented its own __construct(), PHP would only run the constructor of itself, not of its parent. For example:

<?php

class A {
  public function __construct() {
    echo "run A's constructor\n";
  }
}

class B extends A {
  public function __construct() {
    echo "run B's constructor\n";
  }
}

// only B's constructor is invoked
// show "run B's constructor\n" only
$obj = new B();

?>

In this case, if you need to run class A's constructor when $obj is declared, you'll need to use parent::__construct():

<?php

class A {
  public function __construct() {
    echo "run A's constructor\n";
  }
}

class B extends A {
  public function __construct() {
    parent::__construct();
    echo "run B's constructor\n";
  }
}

// both constructors of A and B are invoked
// 1. show "run A's constructor\n"
// 2. show "run B's constructor\n"
$obj = new B();

?>

In CodeIgniter's case, that line runs the constructor in CI_Controller. That constructor method should have helped your controller codes in some way. And you'd just want it to do everythings for you.

like image 123
Koala Yeung Avatar answered Oct 28 '22 12:10

Koala Yeung


I believe the need of calling the parent constructor/method is a code smell, known as Call super. Besides the error-sensitivity (forgetting this call, you can get unexpected results), it's procedural instead of OOP. After all, the order of statements can lead to unexpected results too.

Read more here: https://martinfowler.com/bliki/CallSuper.html

like image 42
schellingerht Avatar answered Oct 28 '22 10:10

schellingerht