0

Question 1

In CodeIgniter... I've seen some weird stuff... For example:

parent::__Construct(); with no parameters passed into it to load into the construct of the extended class.

I assumed it must be from older PHP versions... I don't really understand what the above would do... I only see value if you pass something into the construct of the extending class.

class Sausage
{
    private $name;
    function __Construct( $something )
    {
        $this->name = $something;
    }
}

class Something extends Sausage
{
    parent::__Construct( "Hi" ); 
    echo $this->name; // outputs "Hi"
}

In Codeigniter, they do not pass anything into the parent::_Construct.. So I do not understand what purpose it serves :S

Question 2

Second of all, WHAT ON EARTH does this mean:

self::$instance =& $this;

In the following snippet from Code Igniter:

class CI_Controller {

 private static $instance;

 public function __construct()
 {
  self::$instance =& $this;

make the $instance variable equal the reference of $this?? $this doesn't account for anything in a static context? i'm confused.

Thanks for your help, totally making me go crazy here.

Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233

3 Answers3

0

For the first question, imagine this setup:

class Animal {
    private $alive;
    public function __construct() {
        $this->alive = true;
    }
    public function isAlive() {
        return $this->alive;
    }
    public function kill() {
        $this->alive = false;
    }
}

class Dog extends Animal {
    private $sound;
    public function __construct() {
        $this->sound = "bark";
    }
}

$dog = new Dog;

What happens if we now call $dog->isAlive()? We get null, because $alive was never set. You need to call parent::__construct() in Dog::__construct, otherwise the bootstrapping in the parent function would never take place.

Obviously in a real world situation the constructors would be much more complex, but this should demonstrate why parent::__construct() would be used.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • Ah I see, so basically they are setting stuff in their construct rather than setting it straight off.. because they may wish to make a new declaration in there and that is illegal syntax.. so run the construct.. got ya. – Jimmyt1988 Nov 01 '13 at 16:15
  • @JamesT I'm not quite sure what you mean. Often the constructor has code that the child class needs to run. The way to do that is to invoke the constructor. Sometimes this means passing a method; sometimes it doesn't. – lonesomeday Nov 01 '13 at 16:20
0

Calling parent::__construct() from child means we want the parent's constructor to be invoked along with the child constructor as well when the child object is created .

Passing the parameters depends on the constructor itself. i.e How is constructor defined.

You don't always need to pass a parameter to a function for it to function ;)

AND for your second question this is what i think it is

self::$instance =& $this;

You are making your static variable $instance a refrence variable of your own class i.e $this

themightysapien
  • 8,309
  • 2
  • 17
  • 15
  • Question 1, thanks.. Question 2.. for what reason? Why not in all bits of code calling upon the CI_Controller do they not just access the functions directly: CI_Controller::AFunction()... rather than CI_Controller::$instance::AFunction() – Jimmyt1988 Nov 01 '13 at 16:22
  • for you to access the function like CI_Controller::AFunction() .. the function must be defined as static. Making the instance variable of your own class lets other class to access that class easily. Other class don't need to inherit it or create functions. Just as an example a new class can access CI_Controller class function as CI_Controller::$instance::AnyFunction() from anywhere. – themightysapien Nov 03 '13 at 06:07
0

Following code

parent::__Construct();

Calls the constructor method of the parent class from which current class is derived, parameters not passed because, it doesn't accept any and it's important to call because when you extent a sub class from parent class, you have to call it's constructor explicitly, otherwise, it's constructor won't be called.

Follwing code (targeted version-4)

self::$instance = & $this;

Since, $instance is an static property, to access it they used self::$instance and & $this used to assign the class instance as a reference.

In this code private static $instance; the property is declared as static and it'll keep it's state even after you call the class second time, which means if it contains the reference of the class then it won't assign it again, so you'll always get only one instance of the class and it's known as singleton design pattern, which is known as an anti-pattern, not recommended.

The Alpha
  • 143,660
  • 29
  • 287
  • 307