Adts Qdsfsd
Adts Qdsfsd

Reputation: 121

PHP: Setting variable for parent class

I have

class Meat extends Food {
$var = Food::foodFunction…
}

I need to set $var for the Food class, how can I do that?

Thanks

Upvotes: 3

Views: 9354

Answers (2)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

If $var in the food class is a protected / public variable you just do with $this->var, if it's private you can't set it

Upvotes: 5

nickb
nickb

Reputation: 59699

In the constructor or some other member function by using $this:

function __construct() {
    $this->var = Food::foodFunction();
}

Otherwise, you cannot initialize $var to anything that is not static.

Upvotes: 2

Related Questions