note 113584 added to language.oop5.magic

From: Date: Fri, 01 Nov 2013 04:09:14 +0000
Subject: note 113584 added to language.oop5.magic
Groups: php.notes 
Request: Send a blank email to php-notes+get-197375@lists.php.net to get a copy of this message
<?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 ?> ---- Server IP: 64.71.164.2 Probable Submitter: 31.147.128.168 ---- Manual Page -- http://php.net/manual/en/language.oop5.magic.php Edit -- https://master.php.net/note/edit/113584 Del: integrated -- https://master.php.net/note/delete/113584/integrated Del: useless -- https://master.php.net/note/delete/113584/useless Del: bad code -- https://master.php.net/note/delete/113584/bad+code Del: spam -- https://master.php.net/note/delete/113584/spam Del: non-english -- https://master.php.net/note/delete/113584/non-english Del: in docs -- https://master.php.net/note/delete/113584/in+docs Del: other reasons-- https://master.php.net/note/delete/113584 Reject -- https://master.php.net/note/reject/113584 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#197375) next »