What Are Access Modifiers In PHP OOP And How To Use?

Access modifiers play a crucial role in defining the visibility and accessibility of properties and methods within a class in PHP.

They help in encapsulating and controlling the access to class members, contributing to the principles of encapsulation and information hiding in object-oriented programming (OOP).

In this article, we’ll delve into the three main access modifiers in PHP OOP: public, private, and protected.

Table of Contents #
  1. Introduction to Access Modifiers
  2. Public Access Modifier
  3. Private Access Modifier
  4. Protected Access Modifier
  5. Choosing the Right Access Modifier
  6. Inheritance and Access Modifiers
  7. Conclusion

1. Introduction to Access Modifiers

Access modifiers define the visibility of class members (properties and methods) and control their accessibility from outside the class.

In PHP, there are three main access modifiers:

  • Public: Accessible from anywhere, both within the class and outside of it.
  • Private: Accessible only within the class that defines them.
  • Protected: Accessible within the class and its subclasses (child classes).
PHP access modifiers accessibility public private protected

2. Public Access Modifier

<?php
class MyClass {
    public $publicProperty = 'I am public!';

    public function publicMethod() {
        echo 'This is a public method.';
    }
}

$obj = new MyClass();
echo $obj->publicProperty; // Output: I am public!
$obj->publicMethod(); // Output: This is a public method.

In this example, both the public property and the public method are accessible from outside the class.

3. Private Access Modifier

<?php
class MyClass {
    private $privateProperty = 'I am private!';

    private function privateMethod() {
        echo 'This is a private method.';
    }

    public function accessPrivate() {
        echo $this->privateProperty; // Accessible within the class
        $this->privateMethod(); // Accessible within the class
    }
}

$obj = new MyClass();
// Attempting to access private members will result in an error:
// echo $obj->privateProperty; // Error
// $obj->privateMethod(); // Error

// But we can access them indirectly through a public method:
$obj->accessPrivate(); // Output: I am private! This is a private method.

Private members are not accessible from outside the class. However, you can create public methods within the class to indirectly access private members.

4. Protected Access Modifier

<?php
class MyClass {
    protected $protectedProperty = 'I am protected!';

    protected function protectedMethod() {
        echo 'This is a protected method.';
    }
}

class MyChildClass extends MyClass {
    public function accessProtected() {
        echo $this->protectedProperty; // Accessible within the subclass
        $this->protectedMethod(); // Accessible within the subclass
    }
}

$obj = new MyClass();
// Attempting to access protected members will result in an error:
// echo $obj->protectedProperty; // Error
// $obj->protectedMethod(); // Error

$childObj = new MyChildClass();
$childObj->accessProtected(); // Output: I am protected! This is a protected method.

Protected members are accessible within the class and its subclasses (child classes). In this example, MyChildClass can access the protected members of MyClass.

5. Choosing the Right Access Modifier

  • Public: Use when a property or method should be accessible from anywhere, both within and outside the class.
  • Private: Use when a property or method should only be accessible within the class that defines them.
  • Protected: Use when a property or method should be accessible within the class and its subclasses.

Choose the appropriate access modifier based on the desired level of encapsulation and information hiding.

6. Inheritance and Access Modifiers

Access modifiers also play a role in inheritance. When a subclass extends a parent class, it inherits the properties and methods with public and protected access modifiers. However, private members are not inherited.

<?php
class ParentClass {
    public $publicProperty = 'Public property';
    protected $protectedProperty = 'Protected property';
    private $privateProperty = 'Private property';

    public function publicMethod() {
        echo 'Public method';
    }

    protected function protectedMethod() {
        echo 'Protected method';
    }

    private function privateMethod() {
        echo 'Private method';
    }
}

class ChildClass extends ParentClass {
    public function accessParentProperties() {
        echo $this->publicProperty; // Accessible
        echo $this->protectedProperty; // Accessible
        // Attempting to access private members will result in an error:
        // echo $this->privateProperty; // Error
        // $this->privateMethod(); // Error
    }
}

$childObj = new ChildClass();
$childObj->accessParentProperties();

In this example, ChildClass can access public and protected members of ParentClass through inheritance.

7. Conclusion

Understanding access modifiers is essential for designing well-structured and maintainable object-oriented code in PHP.

By appropriately using public, private, and protected access modifiers, you can control the visibility and accessibility of class members, promoting encapsulation and information hiding.

Choose access modifiers based on the desired level of encapsulation and the needs of your application.