RE: [PHP] about __get,__set Overloading, read-only properties
I think you misread my pure-hearted thankfulness.I am sorry for my ambiguous sentence. Last reply mean : I'm happy to known that we have the same idea, just like a student got teacher's praise . Merry Christmas Eve! -Original Message- From: Jochem Maas [mailto:[EMAIL PROTECTED] Sent: Sunday, December 23, 2007 9:08 AM To: ked Cc: php-general@lists.php.net Subject: Re: [PHP] about __get,__set Overloading, read-only properties ked schreef: My idea is just your answer...happy..ing ^_^ You are so warmhearted. Thanks a lot! : ) I don't get accused of that very often. are you being sarcastic or did my suggestion help? (sorry I didn't quite understand your reply) I will never post a question to a existing thread . good. that's one down 1,000,983 to go :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] about __get,__set Overloading, read-only properties
ked schreef: My idea is just your answer...happy..ing ^_^ You are so warmhearted. Thanks a lot! : ) I don't get accused of that very often. are you being sarcastic or did my suggestion help? (sorry I didn't quite understand your reply) I will never post a question to a existing thread . good. that's one down 1,000,983 to go :-) -Original Message- From: Jochem Maas [mailto:[EMAIL PROTECTED] Sent: Friday, December 21, 2007 9:11 AM To: ked Cc: php-general@lists.php.net Subject: Re: [PHP] about __get,__set Overloading, read-only properties pleae don't reply to an existing thread when posting a new question. ked schreef: Hi. all , I got a article from php 5.0 manual's comments. It's useful, offer readonly properties for classes. (look at the end of this message for the article ) find out function __construct(), I want to modify $this-id in it , then I got a readonly Exception (defined in __set function). Distinctly, a read-only property could not be change via $obj-attribute = '' , but is could be change via $this-id='', inside of class , isn't it ? How to modify __set function ? don't - let __set() be the policeman it's supposed to be. either create a private function to initialize values or set the values directly in the array private function init($k, $v) { if (isset($this-p_arrPublicProperties[$k])) $this-p_arrPublicProperties[$k]['value'] = $v; } thanks for any advises. regards! ked the article is here: -- -- Eric Lafkoff (22-Feb-2006 02:56) If you're wondering how to create read-only properties for your class, the __get() and __set() functions are what you're looking for. You just have to create the framework and code to implement this functionality. Here's a quick example I've written. This code doesn't take advantage of the type attribute in the properties array, but is there for ideas. ?php class Test { private $p_arrPublicProperties = array( id = array(value = 4,type = int,readonly = true), datetime = array(value = Tue 02/21/2006 20:49:23,type = string, readonly = true), data = array(value = foo, type = string, readonly = false) ); //ked add!!! public function __construct() { $this-id = 100; //will get exception !! } private function __get($strProperty) { //Get a property: if (isset($this-p_arrPublicProperties[$strProperty])) { return $this-p_arrPublicProperties[$strProperty][value]; } else { throw new Exception(Property not defined); return false; } } private function __set($strProperty, $varValue) { //Set a property to a value: if (isset($this-p_arrPublicProperties[$strProperty])) { //Check if property is read-only: if ($this-p_arrPublicProperties[$strProperty][readonly]) { throw new Exception(Property is read-only); ///---note here return false; } else { $this-p_arrPublicProperties[$strProperty][value] = $varValue; return true; } } else { throw new Exception(Property not defined); return false; } } private function __isset($strProperty) { //Determine if property is set: return isset($this-p_arrPublicProperties[$strProperty]); } private function __unset($strProperty) { //Unset (remove) a property: unset($this-p_arrPublicProperties[$strProperty]); } } $objTest = new Test(); print $objTest-data . \n; $objTest-data = bar; //Works. print $objTest-data; $objTest-id = 5; //Error: Property is read-only. ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] about __get,__set Overloading, read-only properties
pleae don't reply to an existing thread when posting a new question. ked schreef: Hi. all , I got a article from php 5.0 manual's comments. It's useful, offer readonly properties for classes. (look at the end of this message for the article ) find out function __construct(), I want to modify $this-id in it , then I got a readonly Exception (defined in __set function). Distinctly, a read-only property could not be change via $obj-attribute = '' , but is could be change via $this-id='', inside of class , isn't it ? How to modify __set function ? don't - let __set() be the policeman it's supposed to be. either create a private function to initialize values or set the values directly in the array private function init($k, $v) { if (isset($this-p_arrPublicProperties[$k])) $this-p_arrPublicProperties[$k]['value'] = $v; } thanks for any advises. regards! ked the article is here: Eric Lafkoff (22-Feb-2006 02:56) If you're wondering how to create read-only properties for your class, the __get() and __set() functions are what you're looking for. You just have to create the framework and code to implement this functionality. Here's a quick example I've written. This code doesn't take advantage of the type attribute in the properties array, but is there for ideas. ?php class Test { private $p_arrPublicProperties = array( id = array(value = 4,type = int,readonly = true), datetime = array(value = Tue 02/21/2006 20:49:23,type = string, readonly = true), data = array(value = foo, type = string, readonly = false) ); //ked add!!! public function __construct() { $this-id = 100; //will get exception !! } private function __get($strProperty) { //Get a property: if (isset($this-p_arrPublicProperties[$strProperty])) { return $this-p_arrPublicProperties[$strProperty][value]; } else { throw new Exception(Property not defined); return false; } } private function __set($strProperty, $varValue) { //Set a property to a value: if (isset($this-p_arrPublicProperties[$strProperty])) { //Check if property is read-only: if ($this-p_arrPublicProperties[$strProperty][readonly]) { throw new Exception(Property is read-only); ///---note here return false; } else { $this-p_arrPublicProperties[$strProperty][value] = $varValue; return true; } } else { throw new Exception(Property not defined); return false; } } private function __isset($strProperty) { //Determine if property is set: return isset($this-p_arrPublicProperties[$strProperty]); } private function __unset($strProperty) { //Unset (remove) a property: unset($this-p_arrPublicProperties[$strProperty]); } } $objTest = new Test(); print $objTest-data . \n; $objTest-data = bar; //Works. print $objTest-data; $objTest-id = 5; //Error: Property is read-only. ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] about __get,__set Overloading, read-only properties
My idea is just your answer...happy..ing ^_^ You are so warmhearted. Thanks a lot! : ) I will never post a question to a existing thread . -Original Message- From: Jochem Maas [mailto:[EMAIL PROTECTED] Sent: Friday, December 21, 2007 9:11 AM To: ked Cc: php-general@lists.php.net Subject: Re: [PHP] about __get,__set Overloading, read-only properties pleae don't reply to an existing thread when posting a new question. ked schreef: Hi. all , I got a article from php 5.0 manual's comments. It's useful, offer readonly properties for classes. (look at the end of this message for the article ) find out function __construct(), I want to modify $this-id in it , then I got a readonly Exception (defined in __set function). Distinctly, a read-only property could not be change via $obj-attribute = '' , but is could be change via $this-id='', inside of class , isn't it ? How to modify __set function ? don't - let __set() be the policeman it's supposed to be. either create a private function to initialize values or set the values directly in the array private function init($k, $v) { if (isset($this-p_arrPublicProperties[$k])) $this-p_arrPublicProperties[$k]['value'] = $v; } thanks for any advises. regards! ked the article is here: -- -- Eric Lafkoff (22-Feb-2006 02:56) If you're wondering how to create read-only properties for your class, the __get() and __set() functions are what you're looking for. You just have to create the framework and code to implement this functionality. Here's a quick example I've written. This code doesn't take advantage of the type attribute in the properties array, but is there for ideas. ?php class Test { private $p_arrPublicProperties = array( id = array(value = 4,type = int,readonly = true), datetime = array(value = Tue 02/21/2006 20:49:23,type = string, readonly = true), data = array(value = foo, type = string, readonly = false) ); //ked add!!! public function __construct() { $this-id = 100; //will get exception !! } private function __get($strProperty) { //Get a property: if (isset($this-p_arrPublicProperties[$strProperty])) { return $this-p_arrPublicProperties[$strProperty][value]; } else { throw new Exception(Property not defined); return false; } } private function __set($strProperty, $varValue) { //Set a property to a value: if (isset($this-p_arrPublicProperties[$strProperty])) { //Check if property is read-only: if ($this-p_arrPublicProperties[$strProperty][readonly]) { throw new Exception(Property is read-only); ///---note here return false; } else { $this-p_arrPublicProperties[$strProperty][value] = $varValue; return true; } } else { throw new Exception(Property not defined); return false; } } private function __isset($strProperty) { //Determine if property is set: return isset($this-p_arrPublicProperties[$strProperty]); } private function __unset($strProperty) { //Unset (remove) a property: unset($this-p_arrPublicProperties[$strProperty]); } } $objTest = new Test(); print $objTest-data . \n; $objTest-data = bar; //Works. print $objTest-data; $objTest-id = 5; //Error: Property is read-only. ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php