raindropz12 Posted April 11, 2011 Share Posted April 11, 2011 what are magic methods? when do we need to use magic methods? what are the advantages of using magic methods? thanks in advanced. Quote Link to comment https://forums.phpfreaks.com/topic/233316-magic-methods/ Share on other sites More sharing options...
requinix Posted April 11, 2011 Share Posted April 11, 2011 Is this a homework question? Quote Link to comment https://forums.phpfreaks.com/topic/233316-magic-methods/#findComment-1199896 Share on other sites More sharing options...
raindropz12 Posted April 11, 2011 Author Share Posted April 11, 2011 Is this a homework question? sorry it's not homework question. Quote Link to comment https://forums.phpfreaks.com/topic/233316-magic-methods/#findComment-1199950 Share on other sites More sharing options...
spiderwell Posted April 11, 2011 Share Posted April 11, 2011 abracadabra Quote Link to comment https://forums.phpfreaks.com/topic/233316-magic-methods/#findComment-1199957 Share on other sites More sharing options...
raindropz12 Posted April 11, 2011 Author Share Posted April 11, 2011 I don't understand __set_state. Can you please explain? thank you. Quote Link to comment https://forums.phpfreaks.com/topic/233316-magic-methods/#findComment-1200233 Share on other sites More sharing options...
lastkarrde Posted April 12, 2011 Share Posted April 12, 2011 Well, usually you can only access object properties if they are defined: <?php class Person { public $name; public $age; public $title = 'Sir'; } $p = new Person(); echo $p->title; //Sir $p->name = 'Atticus'; echo $p->name; //Atticus $p->age = 48; echo $p->age; //48 ?> The magic method __set() (usually combined with __get() ) allows you to define and access object properties dynamically. <?php class Person { public $data = array(); public function __set($name, $value) { $this->data[$name] = $value; } public function __get($name) { if(isset($this->data[$name])) { return $this->data[$name]; } else { echo 'NOT SET'; } } } $z = new Person(); echo $z->title; //NOT SET $z->name = 'Atticus'; echo $z->name; //Atticus $z->age = 48; echo $z->age; //48 ?> Quote Link to comment https://forums.phpfreaks.com/topic/233316-magic-methods/#findComment-1200423 Share on other sites More sharing options...
raindropz12 Posted April 15, 2011 Author Share Posted April 15, 2011 Well, usually you can only access object properties if they are defined: <?php class Person { public $name; public $age; public $title = 'Sir'; } $p = new Person(); echo $p->title; //Sir $p->name = 'Atticus'; echo $p->name; //Atticus $p->age = 48; echo $p->age; //48 ?> The magic method __set() (usually combined with __get() ) allows you to define and access object properties dynamically. <?php class Person { public $data = array(); public function __set($name, $value) { $this->data[$name] = $value; } public function __get($name) { if(isset($this->data[$name])) { return $this->data[$name]; } else { echo 'NOT SET'; } } } $z = new Person(); echo $z->title; //NOT SET $z->name = 'Atticus'; echo $z->name; //Atticus $z->age = 48; echo $z->age; //48 ?> did you mean __set_state is the same as __set? Quote Link to comment https://forums.phpfreaks.com/topic/233316-magic-methods/#findComment-1201984 Share on other sites More sharing options...
lastkarrde Posted April 15, 2011 Share Posted April 15, 2011 I've never heard of __set_state() before.. Quote Link to comment https://forums.phpfreaks.com/topic/233316-magic-methods/#findComment-1202153 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.