Call Parent Constructor Before Child Constructor in PHP

Call parent constructor before child constructor in PHP

Just call parent::__construct in the child.

class Form extends Tag
{
function __construct()
{
parent::__construct();
// Called second.
}
}

Is it mandatory to call parent::__construct from the constructor of child class in PHP?

Is it mandatory?

No

Is the above code correct?

Yes

Can we do something before you call the parent's constructor?

Yes. You can do it in any order you please.

Also if we do not call the parent's constructor in the child's constructor like below is it legal?

Yes. But it's not implicit either. If the child constructor doesn't call the parent constructor then it will never be called because the child constructor overrides the parent. If your child has no constructor then the parent constructor will be used

Must I call parent::__construct() in the first line of the constructor?

If you want the code in the parent's constructor to be executed, you need to call parent::__construct(…) at some point. It does not technically matter when you do so. Sometimes it makes more sense to do a little work in the overridden constructor before calling the parent's constructor, sometimes you rely on work the parent's constructor does before you can do work in the overridden constructor.

As a rule of thumb I'd say you should call the parent's constructor as soon as possible. If you need to do something before you can call the parent's constructor, do so. If not, call it immediately. This is to avoid the parent's constructor undoing any of the work you're doing in the overridden constructor, like setting certain properties for example.

class A {
function __construct() {
$this->foo = 'bar';
}
}

class B extends A {
function __construct() {
// parent::__construct();
$this->foo = 'baz';
// parent::__construct();
}
}

In the above sample, the difference between calling the parent first or last makes a big difference in the resulting object. Which is more appropriate depends on what you're trying to do.

How to call parent constructor in PHP OOP?

In your Application class' constructor you have to explicitly call the parent constructor:

public function __construct($Settings){
$this->Router = new \Bramus\Router\Router();
$this->URL = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http")."://".$_SERVER['HTTP_HOST'].'/';
$this->fullURL = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http")."://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
parent::__construct(); // <- Add this
}

extended class constructor not fired when child class is called using call_user_func_array

I'll take a stab in the dark here, and say that you're actually doing this:

call_user_func_array(array('AuthController', 'login'), $data);

In other words, you're not passing an instance to call_user_func, you're just passing the string 'AuthController'. That means your method will get called statically. If you had error reporting and/or strict error reporting enabled, you should see a notice warning you about calling non-static methods statically.

The problem is (probably) that you're never actually instantiating your class, so no constructor is ever run. call_user_func won't instantiate it for you. You'll need to do that yourself:

call_user_func_array(array(new $controller, $route['action']), $data);
// ^^^

How do I get a PHP class constructor to call its parent's parent's constructor?

The ugly workaround would be to pass a boolean param to Papa indicating that you do not wish to parse the code contained in it's constructor. i.e:

// main class that everything inherits
class Grandpa
{
public function __construct()
{

}

}

class Papa extends Grandpa
{
public function __construct($bypass = false)
{
// only perform actions inside if not bypassing
if (!$bypass) {

}
// call Grandpa's constructor
parent::__construct();
}
}

class Kiddo extends Papa
{
public function __construct()
{
$bypassPapa = true;
parent::__construct($bypassPapa);
}
}

Call child method before parent Constructor in PHP

You need to override the parent constructor in the child.

public function __construct(){
$this->setAttr();
}

If you don't override the parent constructor, it will be called when you instantiate the child. However, if you override it, it will only be called if you do parent::__construct() from the child constructor.



Related Topics



Leave a reply



Submit