MemoryRefresh!

Features / Magic methods   Magic methods



Questions Answers 0%

Pages   0 from 91
Questions   0 from 142

Reset



<?php class A { public function __get($var) { return 2; } } $a = new A(); echo $a->foo; // outputs 2 // --- Look Here --- // With __get() method you can keep track of variables which are not defined inside the class.
<?php class myController { private $_modelBroker = array(); public function __get($var) { if (strstr($var, 'model')) { $modelKey = substr($var, 5); // variable is already registered or not if (in_array($modelKey, array_keys($this->_modelBroker))) { return $this->_modelBroker[$modelKey]; } else { eval('$model = new Model_'.$modelKey.'();'); $this->_modelBroker[$modelKey] = $model; return $model; } } } } class Model_Number { public $id = 123; } $controller = new myController(); echo $controller->modelNumber->id; // Output 123 // --- Look Here --- // // __get is called, because modelNumber is undefined The function names bellow are magical in PHP classes.
__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() __debugInfo() You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them.

Related Pages

Introduction
Autoload
Class reflection
Magic methods
Late static binding
Type hinting
Exceptions


0% 100%
 
 
0 pages 91 pages
1) Which function do you use to get property not defined in a class? <?php class A { ? } $a = new A(); echo $a->foo;









<?php class A { public function __get($var) { return 2; } } $a = new A(); echo $a->foo; // outputs 2 // --- Look Here --- // With __get() method you can keep track of variables which are not defined inside the class.
<?php class myController { private $_modelBroker = array(); public function __get($var) { if (strstr($var, 'model')) { $modelKey = substr($var, 5); // variable is already registered or not if (in_array($modelKey, array_keys($this->_modelBroker))) { return $this->_modelBroker[$modelKey]; } else { eval('$model = new Model_'.$modelKey.'();'); $this->_modelBroker[$modelKey] = $model; return $model; } } } } class Model_Number { public $id = 123; } $controller = new myController(); echo $controller->modelNumber->id; // Output 123 // --- Look Here --- // // __get is called, because modelNumber is undefined The function names bellow are magical in PHP classes.
__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() __debugInfo() You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them.

References









Programming

Php
   
Regex
   
MySQL
   
Css
   
jQuery
   
Git
   


References