‫So in the last lesson, we learned how to create a class with properties, methods and a constructor.
‫And in this lesson, I want to talk a little bit more about visibility and access modifiers.
‫So there's basically three access modifiers that we can choose from.
‫We have public which is what we used in the last video.
‫This means that the property or the method that we label as public can be accessed from anywhere.
‫So this is this is actually the default behavior if you don't add a modifier because you can actually
‫leave it off, but it's going to be treated as public, meaning that you can access, like for instance,
‫the name and the email from outside of the class in the global scope now protected is where the property
‫or method can be accessed from within its class or any class that inherits from it.
‫So if you have, let's say, a user class, right, as like a parent, and then you have an employee
‫class as a subclass.
‫So if you have something described in or defined in the user, which is the parent class, then you
‫can also use that in the employee class because it's a direct subclass.
‫All right.
‫But you can't use it outside of that in another class that's not inherited or in the global scope or
‫anything like that, then you have private which the property or method can only be accessed from within
‫the class.
‫It doesn't matter if another class inherits it or anything like that, it can only be within that class.
‫So let's jump into the file where we left off and all I did.
‫I just added a line break here.
‫But I mean it's the same code that we that we've been dealing with.
‫You can also get it from the starter code from this this lesson in the sandbox.
‫Now I want to go over what we just saw in the slides, the access modifiers.
‫And I also want to talk about getters and setters.
‫So we have everything public right now.
‫That's why we're able to access a login because it's public.
‫In fact if we if we leave off any access modifier then it's public by default.
‫So you'll see I can still I can still access that.
‫However, if I were to make this private.
‫And I reload.
‫It's not going to work.
‫It says call to private method user login from the global scope.
‫Okay, I can't access this on the global scope because it's private, which means it can only be accessed
‫within the class.
‫So I'm going to make that back to public.
‫And then what I'm going to do is add another private property up here called status.
‫So let's say private status.
‫And I'll set it to a default value of active.
‫All right.
‫And then again, if I were to come down here and echo out user one or user two status.
‫Then I'm going to get that same error.
‫Cannot access private property.
‫Now there may be cases where you do want to change or or see the status from outside the class.
‫So you can do that with getters and setters, which are just special methods to modify or to access
‫private properties.
‫So let's go under login and let's create a public function and we'll call it get status.
‫And all that's going to do is return.
‫Let's say we want that to return the status.
‫So it would be this.
‫Status.
‫And we can do this because it's private, but we're still in the class, right?
‫This method is within the user class.
‫Now, since this method is public, I can access it from outside the class.
‫So if I say user actually you know what?
‫Let's echo.
‫Instead of returning here, which is what you would normally do.
‫But I'm just going to echo it here just so I don't have to echo it down here.
‫So let's say user two and let's say get status.
‫And if I go ahead and run that, you'll see it says active.
‫Right.
‫So I'm able to get that status even though this is private and I can't access this directly, I can
‫access the public function, which can then access the status property, because this public function
‫is inside of the class, and I can access private properties inside the class.
‫Now what if we want to set that to something?
‫Then we could create a setter.
‫In fact, let me just put a comment here.
‫So we'll say getter.
‫And setter.
‫So now we'll say public.
‫If I can type public function and we'll call this set status.
‫And none of our our methods have any arguments.
‫So let's do that.
‫Let's pass in an argument of status and then we can set this.
‫Status equal to whatever status is passed in.
‫And then I should be able to come down here.
‫I'm going to go above the Get status because I want it to read it afterwards.
‫So let's say set status.
‫And we'll set that to a string called inactive.
‫So now if I reload, you'll see we get inactive because again, set status is public.
‫I'm able to pass in a value and set that private property to that because this is within the class.
‫So hopefully that the whole access modifier thing makes sense.
‫Now we do have a third and that's that's protected right.
‫So if I make this status.
‫Protected.
‫And I try to access it directly.
‫So user we'll say user one status.
‫So if I do that.
‫Then I get the error.
‫Cannot access protected property so I can access it through here, right through the getters and setters.
‫I can also access it through classes that directly inherit from this user class.
‫And we're going to go over inheritance in a little bit.
‫So I'm not going to give you that example right now.
‫But just know if we inherit this user class, like let's say with an employee class, we'll be able
‫to access this status from within that class as well.
‫But if it's private then we won't.
‫We can only access it through the class it belongs to.