This question already has an answer here:
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.
</div>
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.
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
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.