Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
What about the Getters and Setters for the rest of the properties? Do you have to add Getters and Setters for ALL of them? What if you don’t care about doing anything with the property before it gets set. Can we just leave those public?
Documentation
Why Getters and Setters
Some of these are concepts we wont be covering in this course, but here are a few ides:
- Controlling the of behavior associated with getting or setting the property - this allows additional functionality (like validation) to be added more easily later.
- Hiding the internal representation of the property while exposing a property using an alternative representation.
- Allowing the getter/setter to be passed around as lambda expressions rather than values.
- Getters and setters can allow different access levels - for example the get may be public, but the set could be protected.
- Insulating your public interface from change - allowing the public interface to remain constant while the implementation changes without affecting existing consumers.
- Providing a debugging interception point for when a property changes at runtime.
- Improved interoperability with libraries that are designed to operate against property getter/setters - Mocking, Serialization, etc.
- Allowing inheritors to change the semantics of how the property behaves and is exposed by overriding the getter/setter methods.
You're doing great.
0:00
There are four more properties,
two arrays, instructions and tags, and
0:01
two strings, yield and source.
0:06
Do you have to add getters and
setters for all the properties?
0:08
What if you don't care about
doing anything with the property
0:13
before it gets set?
0:15
Can't we just leave
those properties public?
0:17
You could.
0:19
However, there are two big reasons why
that's probably not the best option.
0:21
First of all, consistency.
0:26
Since some of our properties
are accessed through getters and
0:28
setters, it's a good idea to access
all properties the same way.
0:31
Second, just because there isn't anything
we want to do with the incoming data right
0:36
now, that doesn't mean there won't be
something we want to do in the future.
0:41
By setting up the getters and
0:46
setters now, if we want to do something
with that incoming data in the future,
0:47
we won't have to change the way
we interact with the object.
0:52
Let's get started.
0:56
Back in our recipe class, we'll start by
changing the visibility of the rest of
0:58
the properties from public to private.
1:02
Now we can add getters and
setters for those properties.
1:18
The next one in the list is instructions.
1:21
Let's implement this as a single
dimension array of strings.
1:24
We'll start with public
function addInstructions.
1:29
We'll do addInstruction singular so
we can add our instructions one at a time.
1:39
We want to accept a string argument.
1:44
Then we can add this new instruction
to our array of instructions.
1:51
We also need to add a getter method,
2:01
so let's add a public
function getInstructions.
2:04
And then we return $this->instructions.
2:14
Back in our cookbook file,
let's add and display the instructions.
2:20
This is my first instruction.
2:36
We'll add another one too.
2:43
This is my second instruction.
2:51
We can then call an implode
on the get instructions.
2:57
We'll separate this with a new line.
3:04
Let's change this to recipe1 as well.
3:16
Now let's try running our script.
3:18
Hm, line five, let's take a look.
3:27
We're trying to set our source.
3:31
Let's comment out this line.
3:34
Line 11 as well.
3:39
Great, now we see My First Recipe.
3:43
One egg, two cups flour.
3:46
This is my first instruction.
3:49
And this is my second instruction.
3:50
Let's move on to our tags.
3:52
Tags are also a single
dimension array of strings.
3:54
But I want to make sure that all my
tags are lowercase, so let's add that.
3:57
Public function addTag.
4:07
Then we're going to set our array tags,
4:19
To a strtolower($tag).
4:25
And now for our getter,
public function getTags()
4:31
return $this->tags.
4:40
Let's switch back over to the cookbook and
add some tags.
4:45
We can then implode on our tags.
5:12
This time we'll separate it with a comma.
5:18
GetTags().
5:25
And run our script.
5:29
Great, we see our breakfast and
5:32
main course tags added at the end,
and they're lowercase.
5:34
All we have left are the two strings,
yield and source.
5:39
Let's add both of these
before we test it out.
5:43
Since I don't really want to do anything
to yield right now, these will be a basic
5:50
getter and setter that will pass
through directly to our property.
5:53
So we'll do public function setyield.
5:57
We'll pass in the yield and
we'll assign it to our yield property.
6:04
We'll also add our getter.
6:18
And we return our yield property.
6:25
The last property is source.
6:34
This will be either a publication or
a person's name.
6:37
So title case like we did for the title
seems like a good choice for this too.
6:40
Let's add public function setSource.
6:44
We'll pass in the source.
6:50
And then we'll set our property.
6:54
We want to ucwords, or
uppercase words, and pass the source.
6:59
Now let's add the getter.
7:07
Great, let's go back to our cookbook and
add our yield and our source.
7:21
We had our sources up here,
so let's change this.
7:26
setSource and setSource.
7:31
And then let's add our yield.
7:38
SetYield.
7:51
And then echo.
7:59
We also wanna return the source, so echo.
8:06
And now let's run our script.
8:17
We see our yield and source now too.
8:22
We have all the pieces set up.
8:25
We're ready to finish
displaying the entire recipe
8:27
with a little formatting
to enhance the readability.
8:29
You need to sign up for Treehouse in order to download course files.
Sign up