PPPHP
PPPHP

Reputation: 737

Calling constructor inside a method

Somehow my mind went far away from the current problem and I made a terrible mistake.. I called a parent constructor inside a method that just initializes classes properties.. Or did I.. The parent constructor's job was to set the ID value. Well PHP allowed me to do that. But isnt that just wrong? And it looks like I can call classes own constructor in the same way.. Isn't it that constructors should only be allowed to call when creating instances of a class... And they are only called when creating instances..

<?php

    class A {

        public function __construct() {
            echo "Test<br />";
        }

    }

    class B extends A {

        public function test() {
            parent::__construct();
        }

    }

    $b = new B();
    $b->test();

    // OUTPUT:
    // Test
    // Test

?>

EDIT: So the conclusion is that PHP allows you to call the constructor inside a method but it actually does nothing. And that other "TEST" string comes from the base class's constructor.

Upvotes: 4

Views: 4461

Answers (4)

Nick Clark
Nick Clark

Reputation: 4467

I would suggest using an "init" method to handle this kind of behavior. Here is an example:

class A {
    public function __construct() {
        $this->init();
    }
    protected function init() {
        echo "Test<br />";
    }
}

class B extends A {
    public function __construct() {
        // suppress parent constructor 
    }
    public function test() {
        $this->init();
    }
}

Upvotes: 0

RobertPitt
RobertPitt

Reputation: 57268

One word describes the situation exactly.

Override

What happens is that when the parent has a method such as a __construct and the child class does not have that method, when the method is called on the child class it calls the parents method.

For example:

class Parent
{
     public function start(){}
}
class Child extends Parent
{

}

if I call the the start method on the Child class its executes the Parent::Start(), but if Child class has a method called Start() that one would be called as its Overriding the parents version.

Every class has a constructor method, and php executes that to compile the class into an object, now if you have a method called __construct() in a class, PHP Calls the internal construct which compiles the class, and then calls your override constructor.

If you do not have a __construct method in the Child class PHP executes the parent __construct

This is the reason why you get 2 x Test in your Object initialization.

The only time you should be using parent:__cosntruct() is if your Child class requires a user defined constructor, you explicitly call the parent construct to prepare the parent object.

For example:

class Parent
{
     public function __construct()
     {
         //Do work on Parent Class
     }
}

class Child extends Parent
{
    public function __construct()
    {
         //Do Work on Child Class

         parent::__construct();
    }
}

The reason you should only ever call a parent constructor within a child constructor and not a child method is that constructors should only ever be called once, this way enforces that.

Upvotes: 3

vbence
vbence

Reputation: 20343

It is not a good practice, and well-behaved OOP languages usually do not let you do that. Although it is totally OK if you know exactly what you're doing. (And you can document that so other programmers working with that code understand your concept).

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385395

You don't call a parent constructor at arbitrary times. You call a parent constructor only from a child constructor, i.e. whilst the object is being constructed.

If php allows otherwise, then that's simply an artefact of how classes are implemented in the language, and should not be taken to mean that it's ever a good thing to do.

Upvotes: 1

Related Questions