PHP OOP Access Modifiers Explained

Last Updated: 24 Jul, 2024
What is visibility in PHP?

Visibility is a big part of Object Oriented Programming. It allows you to control where your class members can be accessed from, for instance to prevent a certain variable from being modified from outside the class. 

The default visibility in PHP is public, which means that the class members can be accessed from anywhere. This means that declaring the visibility is optional in PHP, since it will just fall back to public even if you do not provide access modifier.

For backwards compatibility, the old way of declaring a class variable, where you would prefix the variable name with the “var” keyword will also default to public visibility. But, this is from PHP 4 and should not be used anymore in future version.

PHP is very simple in this context, because it comes with only 3 different access modifiers: private, protected and public.

Explain Public visibility in PHP.

The first level of visibility is “public.” This level doesn't have any restrictions, which means it can be called in any scope.

This means that any public property of an object can be both retrieved and modified from anywhere in a program.

This level is the default behaviour in PHP when visibility is not declared because of backward-compatibility concerns with PHP 4, which did not have visibility.

Technically any method declaration does not need to be preceded by a visibility keyword, which makes it public.

Also, a property can be defined using the “var” keyword to make it public. But for future compatibility reasons, and so your code is explicit in its intent, you should always use a visibility keyword and not use the var keyword.

Explain Protected visibility in PHP.

The "protected" visibility is second level of visibility in PHP and protected methods and properties can be accessed from inside the class they are declared, or in any class that extends them. They can’t be accessed from outside the class or any other subclass.

Explain Private visibility in PHP.

While protected methods and properties are accessible anywhere in the object, the third level “private” is more restrictive.
A private method or property can’t be accessed by any subclass of the class it is defined in.
If you have a class with a private property and a protected property and then extend that class in the subclass, you can access the protected property, but not the private property.

Explain Property Visibility.

Class properties must be defined as public, protected or private. If it is declared using var, the property will be defined as public.

<?php
class MyClass
{
    public $public = ‘Public’;
    protected $protected = ‘Protected’;
    private $private = ‘Private’;

    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}

$obj = new MyClass();
echo $obj->public; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

class MyClass2 extends MyClass
{
    // We can redeclare the public and protected properties, but not private
    public $public = ‘Public2’;
    protected $protected = ‘Protected2’;
    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}

$obj2 = new MyClass2();
echo $obj2->public; // Works
echo $obj2->protected; // Fatal Error
echo $obj2->private; // Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined
 
Explain Method Visibility.

Class methods may be defined as public, private, or protected. Methods declared without any explicit visibility keyword (public, protected,private) are defined as public.

<?php
class MyClass
{
    // Declare a public constructor
    public function __construct() { }

    // Declare a public method
    public function MyPublic() { }

    // Declare a protected method
    protected function MyProtected() { }

    // Declare a private method
    private function MyPrivate() { }

    // This is public
    function Foo()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate();
    }
}

$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work

class MyClass2 extends MyClass
{
    // This is public
    function Foo2()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate(); // Fatal Error
    }
}

$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Private

class Bar 
{
    public function test() 
    {
        $this->testPrivate();
        $this->testPublic();
    }

    public function testPublic() 
    {
        echo “Bar::testPublic\n”;
    }

    private function testPrivate() 
    {
        echo “Bar::testPrivate\n”;
    }
}

class Foo extends Bar 
{
    public function testPublic() 
    {
        echo “Foo::testPublic\n”;
    }

    private function testPrivate() 
    {
        echo “Foo::testPrivate\n”;
    }
}

$myFoo = new Foo();
$myFoo->test(); 
// Bar::testPrivate 
// Foo::testPublic

Explain Constant Visibility.

As of PHP 7.1.0, class constants may be defined as public, private, or protected. Constants declared without any explicit visibility keyword (public, protected,private) are defined as public.

<?php
class MyClass
{
    // Declare a public constant
    public const MY_PUBLIC = ‘public’;

    // Declare a protected constant
    protected const MY_PROTECTED = ‘protected’;

    // Declare a private constant
    private const MY_PRIVATE = ‘private’;

    public function foo()
    {
        echo self::MY_PUBLIC;
        echo self::MY_PROTECTED;
        echo self::MY_PRIVATE;
    }
}

$myclass = new MyClass();
MyClass::MY_PUBLIC; // Works
MyClass::MY_PROTECTED; // Fatal Error
MyClass::MY_PRIVATE; // Fatal Error
$myclass->foo(); // Public, Protected and Private work

class MyClass2 extends MyClass
{
    // This is public
    function foo2()
    {
        echo self::MY_PUBLIC;
        echo self::MY_PROTECTED;
        echo self::MY_PRIVATE; // Fatal Error
    }
}

$myclass2 = new MyClass2;
echo MyClass2::MY_PUBLIC; // Works
$myclass2->foo2(); // Public and Protected work, not Private

 

Thank You, Please Share.

Recommended Posts

IMAGE

PHP OOP Interfaces Explained

An Interface allows you to create programs that specifies which methods a class must implement, without defining how those methods are implemented.

IMAGE

PHP OOP Abstract Classes Explained

A PHP class that has minimum one abstract method is called as abstract class and cannot be instantiated by itself.

IMAGE

PHP OOP Traits Explained

Traits in PHP, are a medium of implementing code reuse and allow us to reuse sets of functions freely in several independent classes.