What are the Access Modifiers in PHP OOP

What are the Access Modifiers in PHP OOP?

When you define properties and methods inside a class, you can access them in different places apart from inside the class, like – through creating objects, and through the child classes.

So, the access modifiers are used to control the accessibility of the properties and methods, which means the access modifier defines where the properties and methods of a class can be accessed.

Types of PHP access modifiers

There are three types of properties and methods access modifiers in PHP –

Note: A class can access its own members (properties or methods) from within using the $this, and It does not matter that the members are public, protected, or private.

PHP OOP access modifiers public, protected, private

1. Public Access modifier

The public access modifier is the default. When it is assigned to a property or a method, the property or the method can be accessed from anywhere, such as – from outside, from child classes.

Accessing the public properties and methods from outside of the class
<?php
class MyClass{
    public $name = "John";
    
    public function showName(){
        echo $this->name."\n";
    }
}
/** Outside of the Class */
$obj = new MyClass();

$obj->showName();
// Changing the name - John to Mark
$obj->name = 'Mark';
$obj->showName();
John
Mark
Accessing the public properties and methods from child class

In the following code, you can see that we are accessing the public Properties and methods of the Parent Class inside the constructor of the child class.

<?php
class ParentClass{
    public $name = "John";
    
    public function showName(){
        echo $this->name."\n";
    }
}

class ChildClass extends ParentClass{
    public function __construct()
    {
        $this->showName();
        $this->name = 'Bruce';
        $this->showName();
    }
}

$obj = new ChildClass();
John
Bruce

2. Protected Access modifier

When you assign the protected access modifier to a class member (property or method), the class member cannot be accessed from outside of the class but, It can be accessed from child classes.

Protected members cannot be accessed from outside of the class
<?php
class MyClass{
    protected $f_name = "John";
    protected $l_name = "Doe";
    
    protected function fullName(){
        echo $this->f_name." ".$this->l_name;
    }

    public function showName(){
        $this->fullName();
    }
}
/** Outside of the Class */
$obj = new MyClass();
$obj->showName();

/** You cannot do the following
 *
    $obj->f_name = "Mark";
    echo $obj->l_name;
    $obj->fullName();
 * 
 */
John Doe
Protected members are accessible within the child class
<?php
class ParentClass{
    protected $name = "John";
    
    protected function showName(){
        echo $this->name."\n";
    }
}

class ChildClass extends ParentClass{
    public function __construct()
    {
        $this->showName();
        $this->name = 'Bruce';
        $this->showName();
    }
}

$obj = new ChildClass();
John
Bruce

3. Private Access modifier

When you assigned the private access modifiers to the members of a class, the members are only accessible within the class. Neither the objects of the class can access nor the child classes can access the members.

Private example – from outside of the class
<?php
class MyClass{
    private $f_name = "Joseph";
    private $l_name = "Cooley";
    
    private function fullName(){
        echo $this->f_name." ".$this->l_name;
    }

    public function showName(){
        $this->fullName();
    }
}
/** Outside of the Class */
$obj = new MyClass();
$obj->showName();

/** You cannot do the following
 *
    $obj->f_name = "Mark";
    echo $obj->l_name;
    $obj->fullName();
 * 
 */
Joseph Cooley
Private example – from the child class
<?php
class ParentClass{
    private $name = "John";
    
    private function showName(){
        echo $this->name."\n";
    }

    protected function sayHi(){
        echo "Hi From the Parent Class.";
    }
}

class ChildClass extends ParentClass{
    public function __construct()
    {
        $this->sayHi();
        /** You cannot do the following
         * 
            echo $this->name;
            $this->showName();
         * 
         */
    }

}

$obj = new ChildClass();
Hi From the Parent Class.