Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - extended __construct

I was wondering if you could help me out..

I have two classes, one extends the other.. Class B will be extended by various different objects and used for common database interactions.. Now I would like class B to handle its connect and disconnects without direction from class A or any external input..

The problem from what I understand is that an extended class won't automatically run its __construct function.. Is there a way around this?

Thanks in advance..

class a extends b {    public function __construct()    {    }        public function validateStuff()    {       $this->insert_record();    } }  class b {    public function __construct()    {       $this->connect();    }     protected function connect()    {       return true;    }     public function insert_record()    {       return true;    } } 
like image 648
Lee Avatar asked Aug 13 '10 13:08

Lee


People also ask

What is __ construct in PHP?

PHP - The __construct FunctionA constructor allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class. Notice that the construct function starts with two underscores (__)!

What are the __ construct () and __ destruct () methods in a PHP class?

Example# __construct() is the most common magic method in PHP, because it is used to set up a class when it is initialized. The opposite of the __construct() method is the __destruct() method. This method is called when there are no more references to an object that you created or when you force its deletion.

Can PHP class have multiple constructors?

Note: PHP lacks support for declaring multiple constructors of different numbers of parameters for a class unlike languages such as Java.

What is 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".


1 Answers

The parent __construct() method defined in class b will run automatically if you instantiate child class a, unless there is a __construct() method defined in class a.

class a extends b {  }   class b {     public function __construct()     {        echo 'In B Constructor';     }  }   $x = new a(); 

If a __construct() method is defined in class a, then this overrides the use of the __construct() method in class b.... it will run instead of the class b __construct() method

class a extends b {     public function __construct()     {        echo 'In A Constructor';     }  }   class b {     public function __construct()     {        echo 'In B Constructor';     }  }   $x = new a(); 

So if your child class has a __construct() method defined, then you need to explicitly call the constructor for the parent if you want to execute that as well.

class a extends b {     public function __construct()     {        parent::__construct();       echo 'In A Constructor';     }  }   class b {     public function __construct()     {        echo 'In B Constructor';     }  }   $x = new a(); 
like image 152
Mark Baker Avatar answered Sep 21 '22 11:09

Mark Baker