eldan88 Posted August 30, 2013 Share Posted August 30, 2013 (edited) Hey guys. I am little bit confused on when to use __set() and when to use __get(). When using __set(), I am basically setting a property when creating an object. I still can get the value of the property when calling it. With that being said, what would be and ideal way of using the __get() magic method? Just to show you an example, if i set name postal_code to 1111 I can just call that postal code with using the __get() <?php class Classexample { function __set($name,$value) { $this->$name = $value; } } $class = new Classexample; $class->postal_code = 11111; echo $class->postal_code; ?> Edited August 30, 2013 by eldan88 Quote Link to comment Share on other sites More sharing options...
requinix Posted August 30, 2013 Share Posted August 30, 2013 Most of the time you use __get and __set to put the data in a different place. If you're just setting it on the object like normal then there's no point using either because that's the default behavior anyways. Since the data is in a different place, if you implement one method you'll very likely need to implement the other. The exception would be using __get and __set with inaccessible members, like class Example { public $one; protected $two; private $three; public function __get($name) { return $this->$name; } public function __set($name, $value) { $this->$name = $value; } public function test() { // none of these will trigger __get or __set $this->one = 123; echo $this->one; $this->two = 234; echo $this->two; $this->three = 345; echo $this->three; } } class Child extends Example { public function test2() { // will not trigger __get/__set $this->one = 123; echo $this->one; $this->two = 234; echo $this->two; // will because it's not accessible $this->three = 345; echo $this->three; } } $e = new Example(); // will not trigger __get/__set $this->one = 123; echo $this->one; // will because it's not accessible $this->two = 234; echo $this->two; $this->three = 345; echo $this->three;As for "ideal", that depends why you want to use them. Quote Link to comment Share on other sites More sharing options...
kicken Posted August 30, 2013 Share Posted August 30, 2013 As an example of "..put[ting] the data in a different place", take a basic template class for example: class Template { protected $mVars=array(); public function __get($nm){ return isset($this->mVars[$nm])?$this->mVars[$nm]:null; } public function __set($nm,$v){ $this->mVars[$nm] = $v; } public function display($file){ extract($this->mVars); include($file); } } $tpl = new Template(); $tpl->Name = 'kicken'; $tpl->Date = date('Y-m-d'); $tpl->display('somefile.tpl'); The class stores all the variables to be used in the template inside the $mVars property. However, by using __get/__set you allow a user to get/set the variables as if they were real properties on the object. By shoving all the variables inside an array rather than directly on the object you prevent the possibility of someone accidentally overwriting some other property that the class might depend on. Quote Link to comment Share on other sites More sharing options...
eldan88 Posted September 1, 2013 Author Share Posted September 1, 2013 Most of the time you use __get and __set to put the data in a different place. If you're just setting it on the object like normal then there's no point using either because that's the default behavior anyways. Since the data is in a different place, if you implement one method you'll very likely need to implement the other. The exception would be using __get and __set with inaccessible members, like class Example { public $one; protected $two; private $three; public function __get($name) { return $this->$name; } public function __set($name, $value) { $this->$name = $value; } public function test() { // none of these will trigger __get or __set $this->one = 123; echo $this->one; $this->two = 234; echo $this->two; $this->three = 345; echo $this->three; } } class Child extends Example { public function test2() { // will not trigger __get/__set $this->one = 123; echo $this->one; $this->two = 234; echo $this->two; // will because it's not accessible $this->three = 345; echo $this->three; } } $e = new Example(); // will not trigger __get/__set $this->one = 123; echo $this->one; // will because it's not accessible $this->two = 234; echo $this->two; $this->three = 345; echo $this->three;As for "ideal", that depends why you want to use them. Okay. I got you! I see what you have done. You have use __set() to set the values and __get() to get them. In my last example I used __set() to to set and get the values. But thanks. You examples makes a lot of sense!!!! Quote Link to comment Share on other sites More sharing options...
Solution eldan88 Posted September 1, 2013 Author Solution Share Posted September 1, 2013 As an example of "..put[ting] the data in a different place", take a basic template class for example: class Template { protected $mVars=array(); public function __get($nm){ return isset($this->mVars[$nm])?$this->mVars[$nm]:null; } public function __set($nm,$v){ $this->mVars[$nm] = $v; } public function display($file){ extract($this->mVars); include($file); } } $tpl = new Template(); $tpl->Name = 'kicken'; $tpl->Date = date('Y-m-d'); $tpl->display('somefile.tpl'); The class stores all the variables to be used in the template inside the $mVars property. However, by using __get/__set you allow a user to get/set the variables as if they were real properties on the object. By shoving all the variables inside an array rather than directly on the object you prevent the possibility of someone accidentally overwriting some other property that the class might depend on. Gotchya! Makes a lot of sense now! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.