Do You Really Know PHP Class Visibility Scopes?

PHP visibility controls where you can access class methods and properties. Public lets anything reach them, protected locks them to the class and its children, private seals them completely. The video uses Car and SportsCar classes to show how each scope works in WordPress, Magento, and Laravel.
Chapters
Do you think you understand PHP class visibility well? There may be a simple scope change that could break your entire subclass implementation. Even though I've been developing for a long while now, there are times that I still find PHP visibility scopes confusing. But understanding public, private and protected visibility is really important if you want to write clean, maintainable and extensible code. By the end of this video, I really hope you have a much better understanding of visibility and what scopes you can use, what when you can use each, and why you would want to use one over the other. Even though these can be really tough to understand, I really try to break things down as simply as I can. So if something in this video just doesn't make any sense to you, please let me know in the comments below. Okay, let's dive into it and talk about these public, private and protected scopes so we can truly understand them. The first is public visibility. The term public outside of the context of coding just means accessible to everyone. And it means the exact same thing in PHP too. In php, when you declare a property or a method as public, it means that anyone can access it from anywhere. For example, here's a simple car class with a public property named Color and a public method named Set color. Since they are all public, this means that they can be accessed from both inside and outside of the class. Here we've created an object of our car class, set a color using the Set color method, and then access that color property directly. Because the function and property are both public, we can do this now because public methods and properties can be accessed everywhere and without restriction. This can also lead to some unintended consequences if you're not careful about what you're making public. You'll want to use the public visibility for any methods that you need to be widely available or accessible throughout your entire code base. And that's public. It's the simplest to understand of the three scopes. Next is protected visibility. Just like with public visibility, the term protected matches its real world. Meaning when I hear protected, I picture a member of the SWAT team protecting an individual. They can still go places, but only under the presence of a guard. So in php, when you declare a property or method as protected, it means that it can only be accessed from within the class that they live in, and within classes that extend that class so others can access it, but only through an allowed channel. Let's now take our car class and make our color property and set color method protected. Because they are protected, they can't be accessed from from outside of the car class directly. Now let's add a new sports car class that extends car. The sports car class is now considered a subclass of the car class. Within it is a new method that calls our protected set color method under the hood. Now when we create an instance of Sports car and call the public set color method, it will call that protected set color function from the car class. Note that we can't access the set color method on the car directly. If we try to, we will get an error. This is because we can no longer access the color property or set color function from the car class directly from our executed PHP code. Since it is now protected, it can only be accessed from within the car class or from within any descendants of that class, such as sportscar. Protected visibility is great for encapsulating logic. This is the idea of protecting functions from being accessed from other areas of your code. If you have logic that needs to be shielded from the world, but needs to be shared across its inherited classes, protected is an excellent choice. And now on to the last scope, which is private. Private means exactly that. It's private. Picture you in your house with the bedroom door closed, in complete privacy. You have access to the outside world using something like a laptop connected to the Internet, but no one has physical access to you. That's exactly what it means in PHP as well. Here we have our car class again, but this time we've made color and setcolor private. Just like before, private methods and properties are only accessible from inside the class that they are declared in. As you can see, trying to access our setcolor method or color property directly from outside of the class scope just doesn't work. Private methods or properties cannot be accessed or manipulated directly from outside of the class they are declared in, not even in subclasses. Private visibility is used when the method or property is specific only to that class and you don't want it to be available or accessible from any other part of your program. It's critical for proper data hiding and encapsulation, which keeps the internal workings of your class hidden away from the rest of your code base. Unlike with protected subclasses, cannot override private methods. This gives you a stronger guarantee that the method will behave consistently because only the original class can use or change it. Even when Sportscar extends car, it cannot access the private methods or properties declared in car. Private visibility should be used in cases where a method or property should not be overridden and its functionality is very much dependent upon the internal implementation of a class. If you learned something about scopes and visibility so far. Remember to tap that like button to help spread the word to other developers. I appreciate it. So when should you use each type of visibility? Here are some quick tips. Public methods should be used when they need to be accessible throughout your code, or when other parts of your code need to interact with them. For instance, WordPress's plugin ecosystem does this with the install and uninstall methods in a WordPress plugin class. Plugins can be activated or deactivated from anywhere, even outside the class. Similarly, in Magento 2, the add product method within a cart class is public, as it needs to be accessible from a multitude of customer actions across the site. Other PHP frameworks such as Laravel include a render method on their blade templating engine class that is responsible for compiling and rendering a view. Protected methods are great when you want to limit access to some code, but still want to share some functionality within an inherited class. Magento provides an excellent example of this with the Validate method in its form classes where subclasses may override them to include custom validation checks. In the WordPress world, a protected method is implemented in something like an Add widget areas method of a WordPress theme class, which allows child themes to modify widget areas. And in Laravel, the Execute method on Q job classes works the exact same way, as subclasses can define specific logic related to a specific job. Private methods are perfect when they are tied to the internal workings of a class, and these are typically helper methods that only the class itself uses. An example of this in WordPress is a method like load dependencies. It contains all the requires and includes for plugins that aren't meant to be accessed directly from outside the class. Magento has a Connect to database method in a data model class which stays private as subclasses don't need direct access to the database connection. Remember, public methods are like a shop's front end, open to everyone. Protected methods act like a family business passing knowledge and capabilities through a class hierarchy, and private methods are the backend operations where nobody except the class itself needs to know about it. The right visibility is just a careful balance between accessibility and encapsulation. It's more of an art than a fixed rule. Always consider the design of your class, the role it plays in your code base, and then use your best judgment. I created a full course that teaches you all of the basics like this and much more and it's called PHP101. If you want to learn more about PHP, check out this completely free course, right here on YouTube. If you enjoyed this video, remember to tap that, like button and subscribe to my channel for more content. All about programming. Thanks for watching, and until next time, keep coding.










