There is a class:

class MY_Controller extends CI_Controller { } 

and the inheriting class:

 class Auth extends MY_Controller{ public $isLogged = false; } 

In the final class:

 class Home extends MY_Controller { public function __construct(){ var_dump($this->isLogged); 

}}

no access to variable: var_dump ($ this-> isLogged);

Writes:

 Message: Undefined property: Home::$isLogged 

    2 answers 2

    I will describe in more detail by example:

     class MY_Controller extends CI_Controller { public $propertyOfMY_Controller; public methodOfMy_Controller() {} } class Auth extends MY_Controller{ public $propertyOfMY_Controller; // Унаследовано от родителя public methodOfMy_Controller() {} // Унаследовано от родителя public $isLogged = false; // Свойство производного класса, отсутсвует // My_Controller } class Home extends MY_Controller { public $propertyOfMY_Controller; // Унаследовано от MY_Controller public methodOfMy_Controller() {} // Унаследовано от MY_Controller public function __construct(){ var_dump($this->isLogged); // Ошибка: isLogged отсутствует у Home и у My_Controller } } 

    Proper inheritance:

      class Home extends Auth { public $propertyOfMY_Controller; // Унаследовано от MY_Controller public methodOfMy_Controller() {} // Унаследовано от MY_Controller public isLogged = false; // Унаследовано от Auth public function __construct(){ var_dump($this->isLogged); // Теперь isLogged доступен из Home } } 
    • Thank you very much - I understood the meaning - IOleg

    you inherit the Home class from MY_Controller, and in MY_Controller there is no isLogged property

    Write a class Home extends Auth and then everything will work.

    • yes - but after all Auth extends MY_Controller extends the MY_Controller class, therefore Home extends MY_Controller - should have access? - IOleg
    • the bottom line is that in all classes I have to use extends MY_Controller, because MY_Controller is my base class, which I want to extend with other classes - IOleg
    • Well, if you inherit the Home class from the Auth class, it also inherits the properties and methods of My_Controller, since Auth is a derived class from My_Controller - MDJHD
    • @IOleg,> yes - but after all Auth extends MY_Controller extends the MY_Controller class, therefore Home extends MY_Controller - should have access? will not have access. in your example, the Auth and Home classes are independent of each other> the point is that I must use extends MY_Controller in all classes, because MY_Controller - I have a base class that I want to extend with other classes I can do this: either $ this-> put isLogged in MY_Controller or in the class Auth; define a static method that returns isLogged and then call it like this - Auth :: isLogged (); - mountpoint
    • And if in these classes there is the same public variable $ data = array (), which is populated with these classes, but at the output in class Home I get porridge, how to organize it correctly? - IOleg