Back to Top

What is the difference between Public, Private, Protected

PHP Access Modifiers - Public, Protected and Private

In this article, we will learn about class properties access modifier.All the properties and methods used in the classes with the keyword public. This means, that they are publicly available, or in any property of the script: within the class itself, inside and outside of classes that inherit.

But this flexibility may be too misleading and in some cases harmful, not to mention that the only bad public does not allow as per several Design Patterns Due to this reason, there are two other modifiers for visibility: protected and private .

Let’s see the descriptions of all the access modifier available in PHP.

public : the property or method declared as a public is respectively accessible / editable and callable from within the class itself, from inside and outside the class of classes that inherit.

protected : the property or method declared as protected is respectively accessible / editable and callable from within the class itself and from within the classes that inherit, but not accessible from outside the class.

private : the property or method declared as private is respectively accessible / editable and only be called from within the class itself.

Now let’s see some practical examples that will help us to understand the differences between public, protected and private that are obtained from the behavior of properties and methods which are declared with using specific modifiers.


The visibility of public is the simplest and most immediate of the three. Declaring an element as public, you will have complete control on the same in every situation:

We recall also that the constants declared within a class are always considered public and, therefore, accessible from anywhere.


The visibility of protected is more restrictive than the public. The properties and methods are not accessible and editable from outside the class but accessible from the classes that inherit it.

If you try to access or modify a property protected or to call a method protected from outside the class:

All result in a Fatal Error :

Fatal error: Can not access protected property AnotherClass::$a in […] on line 116
Fatal error: Call to protected method MyClass::sayHello () from context ” in […] on line 116


The visibility of private is the most restrictive than the above two public and protected. Properties declared as private can be accessed / modified only within the class that declares them, as well as private methods, can be called only on inside the class.


So the final conclusion from above explanations is to use public modifier or method if you want to access it from outside. To use Protected modifier when you want to access it only by classes that inherit and private is very restricted, accessible inside the own class only.

Access modifier is the concept of OOP PHP for making the application more robust and more maintainable.

Hope this article solve your confusion about access modifiers in PHP OOP.If you have any comment, let me know in comment section.

Comments (1)

  1. hallo
    i like it very good thank

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to use an external URL in WordPress?

Posted on 5 years ago


How to use Session in Joomla

Posted on 6 years ago