I have a file / class :: method, (HelperAdmin.php / HelperAdmin :: menuItem ()) retrieving data from the database for generating the main menu and subsections. In order not to execute the query to the database twice, I created a static array in its class. For some reason, after executing this method (when the menu is really formed and I see it) I cannot get this static array. It looks like this:

class HelperAdmin { static $arrMenuItems; ... public static function menuItem() { ....get $items.... self::$arrMenuItems = $items; return $items; } .... } 

... i.e., if I call the method again:


he returns everything to me as it should. If I want to get an array:


returns null. I would like to hear some hypotheses about the reasons for this. If you think that the idea to get data from a static array is not the best from the point of view of Yii architecture - I will be glad to hear your recommendations.

    2 answers 2

    Hmm .. wrote such code

     class HelperAdmin { static $arrMenuItems; public static function menuItem() { $items=array(1,2,3); self::$arrMenuItems = $items; return $items; } } $items=HelperAdmin::menuItem(); var_dump($items); $items1=HelperAdmin::$arrMenuItems; var_dump($items1); 


     array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } 

    Meaning, I understand that I left the same? call you do the same?

    • Yes. I suspect that the problem is not in PHP / OOP, but in the Yii architecture. Actually, the problem is most likely not, there is no solution to the problem. Here is the sequence diagram: admin.php: 1. received data: $ items = HelperAdmin :: menuItem (); 2. placed in the widget: $ this-> widget (... 'items' => $ items ...); After item 1., in theory, $ items should be stored in a static array of class HelperAdmin 3. place the content of the current view (where I need to re-receive the data): echo $ content; - by this time the method HelperAdmin :: menuItem (); ALREADY was called, but instead of an array, I get null. - srgg67
    1. Sink the menu and similar items in Widget, they were invented for similar purposes
    2. Che for garbage? is php4 still alive?

       <php class MyHelper { private static $_instance; private $_menuItems; public static function getInstance() { if (self::$_instance === null) { self::$_instance = new MyHelper; } return self::$_instance; } public function getMenuItem() { if ($this->_menuItems === null) { $this->_menuItems = array('1','2','3','4'); } return $this->_menuItems; } } 

    And get data on MyHelper::getInstance()->getMenuItem() .