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