rsalumpit Posted July 20, 2009 Share Posted July 20, 2009 Class DIV{ $nom; $denom; Function do_it() { Echo $nom/$denom; } } why and how would you use a setMethod here?what really is set method a i googled it but i dont get a close answer on this question.. Quote Link to comment Share on other sites More sharing options...
Adam Posted July 20, 2009 Share Posted July 20, 2009 Wha? As in http request 'setMethod'? There's no particular reason why you'd use it there. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted July 20, 2009 Share Posted July 20, 2009 A method that will set a class property (variable). Getters are the opposite to setters, they get the value of a class property. Setters and getters are magic methods in php5 __set() __get() used for object overloading. Example: <?php class foo { private $data; public function __set($key, $val) { $this->data[$key] = $val; } public function __get($key) { return $this->data[$key]; } } $x = new foo(); // set a value $x->set('name', 'joe'); // get a value print $x->get('name'); ?> Quote Link to comment Share on other sites More sharing options...
rsalumpit Posted July 20, 2009 Author Share Posted July 20, 2009 A method that will set a class property (variable). Getters are the opposite to setters, they get the value of a class property. Setters and getters are magic methods in php5 __set() __get() used for object overloading. Example: <?php class foo { private $data; public function __set($key, $val) { $this->data[$key] = $val; } public function __get($key) { return $this->data[$key]; } } $x = new foo(); // set a value $x->set('name', 'joe'); // get a value print $x->get('name'); ?> so how can i apply it here this my homework from school setmethod is a new subject for us..thnx for the help man Quote Link to comment Share on other sites More sharing options...
trq Posted July 20, 2009 Share Posted July 20, 2009 neil posted an example, what don't you understand? Quote Link to comment Share on other sites More sharing options...
rsalumpit Posted July 20, 2009 Author Share Posted July 20, 2009 neil posted an example, what don't you understand? i really dont get it on how can i use it on the given php code.. Quote Link to comment Share on other sites More sharing options...
trq Posted July 20, 2009 Share Posted July 20, 2009 Well, I'm not sure it could be explained any simpler. Given your class with two properties, you simply need to make to seter methods to set each property. class Div { private $nom; private $denom; public function setnom($amount) { $this->nom = $amount; } public function setdenom($amount) { $this->denom = $amount; } function do_it() { return $this->nom/$this->denom; } } $div = new Div; $div->setnom = 100; $div->setdenom = 50; echo $div->do_it(); 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.