Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
While the public access modifier allows a code from outside OR inside the class to access the class’s methods and properties, the private modifier prevents access to a class’s methods or properties from any code that is outside the class. In this video we’ll talk about why that can be useful.
More Advanced Title Case
Improved Title Case Function for PHP
Case Converter Bundle on Packagist
Access Modifiers
Access modifiers allows us to control the access, or visibility, of our properties. These access modifiers are public, private, or protected. If declared using var, the property will be defined as public. When declaring a property, the visibility MUST be defined by an access modifier.
Public | Publicly accessible from anywhere, even from outside the scope of the class. |
---|---|
Private | Accessed within the class itself. It protects properties and methods from being access from outside the class. |
Protected | Same as private, except by allowing child (sub) classes to access protected parent (super) properties and methods. |
Don't Allow Property Change
If we wanted to keep someone from changing a property after it’s been set the first time, we could use two different options. Both require that the property itself is set to private. Only allow the property to be set in the __construct method and don't set up another method to change the property. Add a conditional to any method that modifies the property to first check that the property is empty before adding a value.
Using Private Methods
Methods use access modifiers as well. So we can also use private to define a method. We may want to setup a method for formatting a property, but we only want to call that method from the __construct method or another method that combines more logic. For example, we only want to allow updating the user's name if we also verify the user's email.
-
0:00
[SOUND] Great work so far.
-
0:06
You've defined your first class with properties and methods, and
-
0:09
instantiated multiple objects from that single class.
-
0:13
Currently, all the properties and methods are public
-
0:17
which means that any object can access those properties and methods directly.
-
0:23
While this does work,
-
0:24
it doesn't give us any control over how the data is stored or retrieved.
-
0:29
Often, we may want to sanitize or format the data before it's stored in the object.
-
0:35
This is along the same lines as the principle we explored
-
0:38
when we filtered input and escaped output.
-
0:42
Take, for example, the title of our recipe.
-
0:44
We probably want every title to be formatted using title case.
-
0:49
But if we're setting the title directly,
-
0:51
we're at the mercy of whatever data is passed to our property.
-
0:55
This data could contain anything, especially if we're accepting user input.
-
1:00
So instead of setting the property directly, we create another method called
-
1:05
a setter, whose job it is to format the incoming data before setting the property.
-
1:12
Let's take a look at this in work spaces.
-
1:15
Back in our recipe file, we create a new public method.
-
1:22
We'll name this setTitle.
-
1:25
The common practice is to name this with set and then the property name.
-
1:30
This makes it easy to know what this method is doing.
-
1:33
We need to be able to accept an argument containing the title.
-
1:37
So let's name that ($title).
-
1:39
Then we can use this $title =$title.
-
1:45
The keyword, this, before the title, gives us access to the object's property.
-
1:50
And title, without the keyword, this, gives us the passed argument.
-
1:55
So this line sets the value of the title property
-
1:58
to the value that was passed as an argument.
-
2:01
This is exactly the same as if we had accessed the title directly.
-
2:04
But now we can do something with the title before actually setting the value.
-
2:09
We're going to use the function, ucwords.
-
2:12
This stands for uppercase words.
-
2:16
This is not a comprehensive solution, but
-
2:18
it does demonstrate how we could use a setter.
-
2:21
Check the teacher's notes for more robust behaviors.
-
2:25
So, now we can call the set title and pass in a lower case title.
-
2:42
Our method should uppercase the first letter of each word.
-
2:45
Let's give this a try.
-
2:46
Go to View > Show Console.
-
2:50
In our Console we're going to type php classes/recipes.php.
-
2:57
Great, we see My First Recipe by Grandma Holligan.
-
3:02
However, we could still access the title directly and set it to lowercase.
-
3:08
We'll see this with my second title.
-
3:16
Now if we run the script again,
-
3:19
we see that the second recipe title is all lowercase.
-
3:23
We don't want to be able to access the property directly, so
-
3:26
we need to change the access modifier from public to private.
-
3:35
Now we can run our script again.
-
3:39
And this time it tells us that it cannot access private property, title.
-
3:44
Let's change this one to setTitle as well.
-
3:58
Great, now we can set our title, and it will follow the formatting that we set up.
-
4:05
However, setting our property to private
-
4:07
also means that we can't read this title directly, either.
-
4:11
Right now we're calling the display recipe.
-
4:14
Let's try echoing out our title.
-
4:23
We need to set up another method, called a getter.
-
4:27
Getters and setters work together to access private properties.
-
4:31
Let's add a new public method.
-
4:38
We'll name this getTitle.
-
4:44
Just like with setters, the common practice is to name the method with get,
-
4:47
and then the property name.
-
4:50
Also just like setters, I could run any code I want when I call the getter method.
-
4:55
There isn't any additional formatting that I want to do to this property, so
-
4:59
I can just return $this->title.
-
5:06
Now let's call our getTitle instead.
-
5:15
Great, now we see My First Recipe, My First Recipe,
-
5:18
because it's calling the title and then displaying the recipe.
-
5:22
We can use getters and setters to perform any complex formatting or
-
5:27
calculations before saving or returning a parameter.
-
5:31
We could update a database or
-
5:33
alert someone that a new recipe has been created.
-
5:36
We could even use this to keep someone from changing a property
-
5:40
after it's been initially set.
-
5:42
Methods also have access modifiers.
-
5:45
So you can create private methods that can only be used by other methods within
-
5:50
the class, but cannot be accessed directly from outside the class.
-
5:55
In the next video,
-
5:56
we'll demonstrate a more complicated setter using an associative array.
-
6:01
Make sure you check the teacher's notes for more information.
You need to sign up for Treehouse in order to download course files.
Sign up