Available Visibility in PHP Classes

There are 3 type of visibility available in php for controlling your property or method.

Public:

Public method or variable can be accessible from anywhere. I mean from inside the class, out side the class and in child class also.

Private:

Method or property with private visibility can only be accessible inside the class. You can not access private method or variable from outside of your class.

Protected:

Method or variable with protected visibility can only be access in the derived class. Or in other word in child class. Protected will be used in the process of inheritance.

Public:

Public visiblity is least restricted visibility available in php.

If you will not define the visibity factor with your method or property then public will be by defautl applied.

Public methods or variables can be accessible from anywhere

.For example,  It can be accessible from using object(outside the class), or inside the class, or in child class.

[pastacode lang=”php” manual=”class%20test%0A%7B%0Apublic%20%24abc%3B%0Apublic%20%24xyz%3B%0Apublic%20function%20xyz()%0A%7B%0A%7D%0A%7D%0A%24objA%20%3D%20new%20test()%3B%0Aecho%20%24objA-%3Eabc%3B%2F%2Faccessible%20from%20outside%0A%24objA-%3Exyz()%3B%2F%2Fpublic%20method%20of%20the%20class%20test%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

Private:

Private method or properties can only be accessible within the class.

You can not access private variable or function of the class by making object out side the class.

But you can use private function and property within the class using $this object.

Private visibility in php classes is used when you do not want your property or function to be exposed outside the class.

example of Private visibility in php classes.

[pastacode lang=”php” manual=”%0AClass%20test%0A%7B%0Apublic%20%24abc%3B%0Aprivate%20%24xyz%3B%0Apublic%20function%20pubDo(%24a)%0A%7B%0Aecho%20%24a%3B%0A%7D%0Aprivate%20function%20privDo(%24b)%0A%7B%0Aecho%20%24b%3B%0A%7D%0Apublic%20function%20pubPrivDo()%0A%7B%0A%24this-%3Exyz%20%3D%201%3B%0A%24this-%3EprivDo(1)%3B%0A%7D%0A%7D%0A%24objT%20%3D%20new%20test()%3B%0A%24objT-%3Eabc%20%3D%203%3B%2F%2FWorks%20fine%0A%24objT-%3Exyz%20%3D%201%3B%2F%2FThrow%20fatal%20error%20of%20visibility%0A%24objT-%3EpubDo(%22test%22)%3B%2F%2FPrint%20%22test%22%0A%24objT-%3EprivDo(1)%3B%2F%2FFatal%20error%20of%20visibility%0A%24objT-%3EpubPrivDo()%3B%2F%2FWithin%20this%20method%20private%20function%20privDo%20and%20variable%20xyz%20is%20called%20using%20%24this%20variable.%0A” message=”Php Code” highlight=”” provider=”manual”/]

[ad type=”banner”]

Protected:

Protected visibility in php classes are only useful in case of inheritance and interface.

We will discuss in depth of interfaces and inheritance in other chapter of this tutorial.

Protected method or variable can be accessible either within class or child class. Here we will take very basic example:

[pastacode lang=”php” manual=”class%20parent%0A%7B%0Aprotected%20%24pr%3B%0Apublic%20%24a%0Aprotected%20function%20testParent()%0A%7B%0Aecho%20this%20is%20test%3B%0A%7D%0A%7D%0Aclass%20child%20extends%20parent%0A%7B%0Apublic%20function%20testChild()%0A%7B%0A%24this-%3EtestParent()%3B%20%2F%2Fwill%20work%20because%20it%0A%7D%0A%7D%0A%24objParent%20%3D%20new%20parent()%3B%0A%24objParent-%3EtestParent()%3B%2F%2FThrow%20error%0A%24objChild%20%3D%20new%20Child()%3B%0A%24objChild-%3EsetChild()%3B%2F%2Fwork%20because%20test%20child%20will%20call%20test%20parent.%0A” message=”Php Code” highlight=”” provider=”manual”/]

If you will take analyze above section you can found that method test Parent() is not accessible from object of class. But it is accessible in child class.

Categorized in: