note 98442 added to language.oop5.magic

From: Date: Tue, 15 Jun 2010 22:26:25 +0000
Subject: note 98442 added to language.oop5.magic
Groups: php.notes 
Request: Send a blank email to php-notes+get-170160@lists.php.net to get a copy of this message
Properties can be used for getter and setter as C# methods get_PropertyName and set_PropertyName for full event-based programming. <?php /** * @author Tufan Baris YILDIRIM * @name getter and setter magics. */ Class GetSet { public $Name,$Surname; // they will be deleted those only for autocompletion. private $_Name,$_Surname; public function __construct() { foreach(get_class_vars('GetSet') as $varName=>$varValue) { if(substr($varName,0,1)!="_") unset($this->$varName); } } public function __get($varName) { if(method_exists($this,$MethodName='get_'.$varName)) return $this->$MethodName(); else trigger_error($varName.' is not avaliable .',E_USER_ERROR); } public function __set($varName,$value) { if(method_exists($this,$MethodName='set_'.$varName)) return $this->$MethodName($value); else trigger_error($varName.' is not avaliable .',E_USER_ERROR); } private function set_Surname($value) { // On surname changed Events. echo "Surname Changed as ".$value."\r\n"; $this->_Surname=$value; } private function get_Surname() { // On Get Events. echo "Surname Getted\r\n"; return $this->_Surname; } private function set_Name($value) { // On Name changed Events. echo "Name Changed as ".$value."\r\n"; $this->_Name=$value; } private function get_Name() { // On Get Name Events. echo "Name getted\r\n"; return $this->_Name; } } ?> Using : <?php $get=new GetSet(); $get->Name="Tufan Baris"; $get->Surname="YILDIRIM"; echo $get->Name.' '.$get->Surname; ?> Output Name Changed as Tufan Baris Surname Changed asYILDIRIM Name getted Surname Getted Tufan Baris YILDIRIM ---- Server IP: 69.147.83.197 Probable Submitter: 88.247.50.230 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php Edit -- https://master.php.net/note/edit/98442 Del: integrated -- https://master.php.net/note/delete/98442/integrated Del: useless -- https://master.php.net/note/delete/98442/useless Del: bad code -- https://master.php.net/note/delete/98442/bad+code Del: spam -- https://master.php.net/note/delete/98442/spam Del: non-english -- https://master.php.net/note/delete/98442/non-english Del: in docs -- https://master.php.net/note/delete/98442/in+docs Del: other reasons-- https://master.php.net/note/delete/98442 Reject -- https://master.php.net/note/reject/98442 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#170160) next »