Есть класс: testclass { мотоды: function method1 { return true; } function method2 { if($this->method1 == false) { Echo "Ой"; } else { return true; } } Расшираем класс: test2class extends testclass { методы: function method3 { if(parent::method2 == false) { echo 'Оёёй'; } } Вызываем: if(test2class::method3() == true) { echo 'Все ок'; } /// Идёт ошибка на то, что строка - if($this->method1 == false) " Использование $this, когда он не объект контекста," - Как устранить эту ошибку? 

    1 answer 1

    This line:

     if(parent::method2 == false) 

    suggests that a static method is used, therefore, no $ this is out of the question.

    It is worth remaking in the method method2:

     if(static::method1() == false) //либо if(self::method1() == false), если необходимо использовать именно родительский метод 

    and in method3 method:

     if(static::method2() == false) 

    and then it will work.

    Why static :: method2 (), if the method method2 is described in the parent class? Because in this example in the derived class you do not overlap it.

    In addition, method2 cannot be declared private, because otherwise, you cannot call it from derived classes.

    • I did not finish reading the example: test2class :: method3 (). This is also a static class call, so you are operating with a class, not an object. $ this points to a specific class object, so it cannot be used in a static call. Instead, use static variables, for example, in the protected scope. Edited an example in response - BOPOH
    • Thanks, helped. - angers777