V
V
vitaly_742020-04-29 08:04:00
PHP
vitaly_74, 2020-04-29 08:04:00

If I inherit from the parent class, how will this method work?

public function current_user(): User{
       if (is_null($this->current_user)){
           /**@var User $record*/
           $record = User::find()->where(['id'=>$this->user_id])->one();
           if ($record){
               $this->current_user = $record;
           }else{
               $this->current_user = new User;
           }
       }
       return $this->current_user;
    }


The question is: if I call this method in the child class, will it take #this from the parent class or will it consider this as the child class? (I do not override this method in the child class)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2020-04-29
@vitaly_74

When inheriting, the child method replaces the parent one, you can refer to the parent implementation through parent. More details: https://www.php.net/manual/ru/language.oop5.inheri...
And in general, read the entire section on php.net for at least a basic understanding of how classes and OOP work in php. Will remove a lot of questions)

The question is: if I call this method in the child class, will it take #this from the parent class or will it consider this as the child class? (I do not override this method in the child class)

In a child class, this points to the parent + current child combination. That is, you can call parent classes from a child, use parent variables, but everything is within visibility (public, protected). Private methods and variables are only available in the current class (parent or child)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question