There is __construct () equivalent for extended classes

I have a parent class:

Abstract Class Base {
    function __construct($registry) {
        // stuff
    }
}

Then I have at least 2 classes that extend this class

class Cms extends Base {
   // a few functions here
}

and this one

class Index extends Base {
   // a few functions here
}

For the CMS class, the user will have to log in. The session is already running, and I want to verify that the user is logged in using the var session. So I would like to do this:

class Cms extends Base {
   function __construct()
   {
        // check the user session is valid
   }
   // a few functions here
}

but of course, this will overwrite the parent class of the construct and everything that it defines. Is there a way to have a method like __construct () that can be defined in a child class?

+3
source share
3 answers

In the child constructor, you can call the parent:

class Cms extends Base {
   function __construct()
   {
       parent::__construct();
        // check the user session is valid
   }
   // a few functions here
}

. ():

. , .
, , parent::__construct() .

+8

php. . ( .)

class Cms extends Base {
   function __construct()
   {
       parent::__construct(); 
       // check the user session is valid
   }
   // a few functions here
}

PHP: (::) :

, PHP . , . , .
+6

php , .. __set(), ( parent:: __), , , .

VolkerK: ? , , , . , , , , . OO ?

0

Source: https://habr.com/ru/post/1726224/


All Articles