D
D
daMage2014-06-10 15:05:20
PHP
daMage, 2014-06-10 15:05:20

How to implement property inheritance?

There are two classes: Parent and Child. Child will inherit the properties and methods of the parent. The parent has one property that is initialized when the class object is created. Later, in the parent, I create a "child" object.

class Parent {
  protected $var = 0;

  public function __construct($var=100) {
    $this->var = $var;
  }

  protected function say() {
    echo $this->var;
  }
}
class Child extends Parent {
  public function __construct() {
    $this->say();
  }
}

In general, when creating an object of the Child class, 0 is displayed, but 100 is needed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2014-06-10
@daMage

Call the constructor of the parent class in the constructor of the child class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question