H
H
hrvasiliy2015-11-25 11:08:46
PHP
hrvasiliy, 2015-11-25 11:08:46

What or how to replace the constructor?

There is a base controller class in the constructor of which the user's authorization is checked. There was a problem when creating child classes: If I create a new constructor in child classes, it overrides the base class constructor.
Why am I trying to use a constructor in child classes? - One example is the creation of a specific page model object.
Is it possible to somehow replace the constructor? Or maybe there is some other way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Hog, 2015-11-25
@hrvasiliy

class A {
  public $firstname;

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

class B extends  A {

  public $lastname;

  public function __construct($firstname,$lastname) {
    parent::__construct($firstname);
    $this->lastname= $lastname;
  }
}

$class = new B('Vasya','Pupkin');

echo $class->firstname , $class->lastname;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question