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 trial

PHP Object-Oriented PHP Basics Building the Recipe Adding Getters and Setters

thimoschroeder
thimoschroeder
23,470 Points

PHP: 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
Simon Coates
28,694 Points

add 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)

Simon Coates
Simon Coates
28,694 Points

Set 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.

Georgios Prassas
Georgios Prassas
261 Points

For 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
thimoschroeder
23,470 Points

Simon 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
Simon Coates
28,694 Points

yes. 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
thimoschroeder
23,470 Points

Awesome, 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
Simon Coates
28,694 Points

$arr = []; creates an array with no values.

Zeljko Porobija
Zeljko Porobija
11,491 Points

As 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.