Stevanicus
Stevanicus

Reputation: 7761

PHP Inheritance and setting variables in parent constuct

Hi I'm wondering if anyone could shed some light on the following problem for me.

I have two classes

class 1

class application{
    private $something = "hello my name is";

    function __construct(){
       $this->something .= " bob";
       $this->require_login_class_method();
    }

    public function getName(){
        return $this->something;
    }

    ...
}

class 2

class login extends application{
    function __construct(){
       echo $this->getName();
    }
}

my result is always

"my name is "

without

"bob"

however if I call (within class login - construct)

parent::__construct();

it works.

What if my application class construct method accepts variables that I dont want to pass or dont have to pass a second time (from login)?

Thanks in advance


Solution

Thank to everyone who responded.

The solution I have gathered so far is to do the following

class application

//if variables are present, make them optional
function __construct($var1 = null, $var2 = null){
    //do something
}

class login

function __construct(){
    parent::__construct();
    //do something with variables set in parent construct
}

Upvotes: 2

Views: 3309

Answers (5)

troelskn
troelskn

Reputation: 117447

You set the variable in the constructor. So you need to call that first. Eg.:

class login extends application{
    function __construct(){
       parent::__construct();
       echo $this->getName();
    }
}

If you need to pass variables through, you must do so manually:

class login extends application{
    function __construct($x){
       parent::__construct($x);
       echo $this->getName();
    }
}

Yes, it's a bit of a clutch. Try not to override constructors, if you can help it. Also, try to avoid doing any heavy logic (Like logging in to a remote service) in a constructor. Better to do that later, on demand.

Upvotes: 1

jeroen
jeroen

Reputation: 91734

I think strict standards requires your child constructor to accept the same variables as its parent, but if you want to get around that, you can always make the variables optional in the parent constructor:

function __construct($name = " bob"){
   $this->something .= $name;
   $this->require_login_class_method();
}

And of course, if you override the constructor, you will have to call it manually from the child if you want to call it at all.

Upvotes: 2

Itay Moav -Malimovka
Itay Moav -Malimovka

Reputation: 53597

In OO you can overwrite methods. So, you overwrote the parent class (means it is not executed when you call the child class).
You have the ability to call any parent methods parent::anyMethod()

This is how it works.

Upvotes: 2

TimWolla
TimWolla

Reputation: 32691

The parent-__constructor has to be called with every argument that has no default value set. If you don't want to set certain variables you have to set default values in your parent-constructor or reimplement the whole code.

Upvotes: 1

Nanne
Nanne

Reputation: 64399

If you extend a class you can override the constructor. If you do, you have to do all the work that is done in that constructor for yourself. You can also call the parent constructor, and have all the work done there called too. There is not really a 3rd option: either you call it, or your don't.

Upvotes: 4

Related Questions