How to Get a PHP Class Constructor to Call Its Parent's Parent's Constructor

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);
}
}

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.

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

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
}

How do I call the parent classes constructor with its original parameters in PHP

One solution would be to not override the parent constructor in the first place. Instead, define a separate (initially-empty) init() method that the parent constructor calls automatically. That method could then be overwritten in the child in order to perform the extra processing.

class A {
public function __construct($para) {
// parent processing using $para values

// ..and then run any extra child initialization
$this->init();
}
protected function init() {
}
}

class B extends A {
protected function init() {
// Additional actions that do not need direct access to the parameters
}
}

Calling parent class constructor in PHP

Your constructor needs pass all needed objects to the parent constuctor. The parent constructor needs a Manager object, so you must pass it in if you want to call it. If DataRepositoryInterface is not a Manager, you'll need to pass a manager into your childs constructor or instantiate an object the necessary class to pass to the parent.

 class DataController extends APIController implements APIInterface {

protected $data;

public function __construct(Manager $fractal, DataRepositoryInterface $data) {
parent::__construct($fractal);
$this->data = $data;
}
}

Or you could instantiate a Manager inside your constuctor

     class DataController extends APIController implements APIInterface {

protected $data;

public function __construct(DataRepositoryInterface $data) {
$fractal = new Manager(); //or whatever gets an instance of a manager
parent::__construct($fractal);
$this->data = $data;
}
}

Calling parent's constructor in PHP

Just call it using parent::

    /* Settings */
class Settings{
function __CONSTRUCT(){
echo "Settings Construct";
}
}

/* PageManager */
class PageManager extends Settings{
function __CONSTRUCT(){
parent::__CONSTRUCT();
echo "PageManager Construct";
}
}

Have a look at the manual(Constructors and Destructors)!



Related Topics



Leave a reply



Submit