note 113584 rejected from language.oop5.magic by tularis

From: Date: Fri, 01 Nov 2013 12:05:13 +0000
Subject: note 113584 rejected from language.oop5.magic by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-197380@lists.php.net to get a copy of this message
Note Submitter: zeljko dot srbljinovic at gmail dot com ---- <?php //This is an example for creating nested classes in PHP. //GLOBAL FUNCTION - is used to put nested classes to work: function subclasses(&$subclasses,&$subclass,$varname)//(NEEDED FOR NESTING TO WORK) { if (isset($subclasses[$varname])) //Is $varname listed as a subclass? { if (!isset($subclass[$varname])) { //If subclass handle is not created, //subclass instance is being created and remembered: $subclass[$varname]=new $subclasses[$varname]; } //Returning a handle of a subclass $varname, for future calls: return $subclass[$varname]; } else { //Subclass with the name '$varname' not defined: return 0; } } //CLASSES DEFINITIONS (examples): class clkey { //propertie: public $code="123"; } class cldoors { //next level classes definitins: private $subclasses=array("key"=>"clkey");//(NEEDED FOR NESTING TO WORK) private $subclass=array();//(NEEDED FOR NESTING TO WORK) //properties: private $color; private $material; //method: public function lock() { echo "House locked"; } public function __get($varname) { //properties: switch($varname) { case "color": return "White"; break; case "material": return "Wood"; break; } //next level classes: return subclasses($this->subclasses,$this->subclass,$varname);//(NEEDED FOR NESTING TO WORK) } } class clwindows { //method: public function open() { echo "Windows opened"; } } class clhouse { //next level classes definitins: private $subclasses=array("door"=>"cldoors","windows"=>"clwindows");//(NEEDED FOR NESTING TO WORK) private $subclass=array();//(NEEDED FOR NESTING TO WORK) //propertie: public $address="Home street 81"; //method: public function lock() { echo "House locked"; } public function __get($varname) { //next level classes: return subclasses($this->subclasses,$this->subclass,$varname);//(NEEDED FOR NESTING TO WORK) } } //TESTING CLASSES NESTING - EXAMPLE: $house=new clhouse(); echo ("Requesting address: "); echo ($house->address."<br>"); echo ("Opening windows: "); echo ($house->windows->open()."<br>"); echo ("Locking the house: "); echo ($house->lock()."<br>"); echo ("Oh, nice door. Color: "); echo ($house->door->color); echo (". Material: "); echo ($house->door->material."<br>"); echo ("Getting the access key code: "); echo ($house->door->key->code."<br>"); echo ("Wooow. Bad code. Changng code to: 173582.<br>"); $house->door->key->code="173582"; echo ("Getting the new access key code: "); echo ($house->door->key->code."<br>"); //RESULT: //Requesting address: Home street 81 //Opening windows: Windows opened //Locking the house: House locked //Oh, nice door. Color: White. Material: Wood //Getting the access key code: 123 //Wooow. Bad code. Changng code to: 173582. //Getting the new access key code: 173582 ?>

« previous php.notes (#197380) next »