Mr Chris Posted July 19, 2010 Share Posted July 19, 2010 Hello All, I have a quick question on getters and setters in the below classes. The classes below are exactly the same, return the same values, but named different methods? Now are these examples both an examples of getter and setter methods? <?php class Box { private $contents; public function setContents($contents) { $this->contents = $contents; } public function getContents() { return $this->contents; } } $box01 = new Box; $box01->setContents('I am contents of box01!'); echo $box01->getContents(); // Output: I am contents of box01! ?> <?php class Box { private $contents; public function produceContents($contents) { $this->contents = $contents; } public function outputContents() { return $this->contents; } } $box01 = new Box; $box01->produceContents('I am contents of box01!'); echo $box01->outputContents(); // Output: I am contents of box01! ?> Get methods don't have to start with the word get and set methods don't have to start with set right? The two classes above are exactly the same apart from the naming convensions. I think i'm getting confused with the magic method __get. That's totally different right? Is it best practice to use the naming convention setWhatever and getWhatever when dealing with objects? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/208204-getter-and-setter-methods/ Share on other sites More sharing options...
lemmin Posted July 19, 2010 Share Posted July 19, 2010 You can name getter and setter methods whatever you want. starting them with "get" and "set" respectively is the standard naming convention. Quote Link to comment https://forums.phpfreaks.com/topic/208204-getter-and-setter-methods/#findComment-1088273 Share on other sites More sharing options...
Maq Posted July 19, 2010 Share Posted July 19, 2010 I think i'm getting confused with the magic method __get. That's totally different right? http://us2.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members I have a quick question on getters and setters in the below classes. The classes below are exactly the same, return the same values, but named different methods? Right, but what's the point of disrupting the naming conventions? It just makes everything confusing especially if other people are working with your code. Also, your method name "outputContents()" isn't an accurate name. You're returning (getting) $contents, not outputting it... Quote Link to comment https://forums.phpfreaks.com/topic/208204-getter-and-setter-methods/#findComment-1088310 Share on other sites More sharing options...
katierosy Posted July 22, 2010 Share Posted July 22, 2010 This link as below talks about property procedure let,get,set and it's rule. http://editorial.co.in/software/vbscript_property_let_get_set.php In PHP such let,get,set procedures may not be available. Nonetheless it works as you have mentioned get,set methods to do similar tasks. Quote Link to comment https://forums.phpfreaks.com/topic/208204-getter-and-setter-methods/#findComment-1089436 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.