PHP - extended __construct
Asked Answered
K

3

46

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;
   }
}
Kane answered 13/8, 2010 at 13:26 Comment(0)
P
117

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();
Pinprick answered 13/8, 2010 at 14:1 Comment(1)
Thanks :) - stackoverflow is so good at having and finding relevant information you can indulge in such workflows as "like, I know this but I just want to make sure that I'm not running into a little PHP intuition quirk somewhere" - thanks haha!Examen
T
14

I'm not sure I fully understand what you are asking, but you can call the parents construct method from the child's constructor

parent::__construct();

That's the only option I know of.

Tincal answered 13/8, 2010 at 13:29 Comment(1)
I didn't want to call the constructor at all from class a.. I was hoping to find a method to apply class b's constructor to run automatically.Kane
O
6

Call parent::__construct() in a::__construct():

class a extends b
{
   public function __construct()
   {
       parent::__construct();
   }   

   public function validateStuff()
   {
      $this->insert_record();
   }
}

You can omit a's constructor altogether if you're not doing any a-specific stuff.

Obtain answered 13/8, 2010 at 13:29 Comment(2)
I didn't want to call the constructor at all from class a.. I was hoping to find a method to apply class b's constructor to run automatically..Kane
Ahh this was the answer.. by leaving the constructor out of class A, class B's ran! Thanks!Kane

© 2022 - 2024 — McMap. All rights reserved.