Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialthimoschroeder
23,470 PointsPHP: Difference between 'add' and 'set' - such as 'setTitle' and 'addIngredients'?
Hi, as pointed out above, my question relates to the concept of setters & getters. The 'set' type is used to access private methods - as well as the 'get', such as setTitle and getTitle. However, when do I use addName and getName? I hope someone can elaborate on this by providing a simple example to clarify this mystery.
Any help is highly appreciated !! :-)
6 Answers
Simon Coates
28,694 Pointsadd might be more common if adding something to an array, or where there is a concept of a collection. 'put' is common if your class is map-like. (you can look at an API to see how the verbs relate to the idea represented by the class. The standard java api is pretty browsable in the event you want to see method naming conventions. if you look at the documentation for the JFrame object for example, you set and get individual properties, but you add child objects)
Georgios Prassas
261 PointsFor me method addIngredient should have been named as setIngredient for two main reasons, first this is an educational video so the viewers are learning concepts and getting confused, and secondly it is a setter because it sets the values of the array ingredients.
thimoschroeder
23,470 PointsSimon Coates - Thanks Simon, highly appreciated, I have an additional question for clarification: When using a variable in such a way:
$arr = [];
It is used to add additional elements to the array? I'm not fully grasping the concepts of associate arrays in the videos yet.
Simon Coates
28,694 Pointsyes. i think so. But since it hasn't got a key value, it assigns itself a numeric key value (i think the highest numeric index +1).
thimoschroeder
23,470 PointsAwesome, thanks for your quick reply. You remind me, I've seen it in one of the instructions. I just stumbled upon another version of this array - one of your answers in the forum:
$arr = [];
Does this imply the same? The variable $arr is added to the existing array with a numeric key value?
Simon Coates
28,694 Points$arr = []; creates an array with no values.
thimoschroeder
23,470 PointsOk thanks !
Zeljko Porobija
11,491 PointsAs you might have already noticed, the real difference between setters and getters is not in the names (although it wouldn't be reasonable to name them in some non-intuitive way), but in what they do. A getter has return $this->someProperty while the setter just defines that someProperty.
Simon Coates
28,694 PointsSimon Coates
28,694 PointsSet is particular. You set a particular field. You might set a particular element in a collection. Add is when you throw the element in at the end, or where you don't care about order. It might be illuminating to google java's map, set, list classes to see the verbs. Class that represent or wrap collections might also use push, pull, pop etc terminology.