PHP OOP – Access Modifiers

In PHP OOP, access modifiers are keywords that are used to restrict the visibility of properties and methods in a class. There are three access modifiers in PHP OOP: public, private, and protected.

  • public: A public property or method can be accessed from outside the class, as well as from within the class.
  • private: A private property or method can only be accessed from within the class.
  • protected: A protected property or method can be accessed from within the class and its subclasses, but not from outside the class.

Here’s an example of a class with public, private, and protected properties and methods:

class Example {
    public $public_property;
    private $private_property;
    protected $protected_property;
    
    public function __construct($public, $private, $protected) {
        $this->public_property = $public;
        $this->private_property = $private;
        $this->protected_property = $protected;
    }
    
    public function get_public_property() {
        return $this->public_property;
    }
    
    private function get_private_property() {
        return $this->private_property;
    }
    
    protected function get_protected_property() {
        return $this->protected_property;
    }
}
$example = new Example("Public", "Private", "Protected");
echo $example->public_property; // Output: Public
echo $example->get_public_property(); // Output: Public
echo $example->private_property; // Error: Cannot access private property Example::$private_property
echo $example->get_private_property(); // Error: Call to private method Example::get_private_property() from context ''
echo $example->protected_property; // Error: Cannot access protected property Example::$protected_property
echo $example->get_protected_property(); // Error: Call to protected method Example::get_protected_property() from context ''

In this example, the Example class has public, private, and protected properties, as well as public, private, and protected methods. The constructor initializes the properties with the values passed to it.

To access the public property and method, we can simply use the arrow (->) operator:

echo $example->public_property; // Output: Public
echo $example->get_public_property(); // Output: Public

To access the private and protected properties and methods, we cannot use the arrow operator, but we can use public methods that have access to these properties and methods:

echo $example->private_property; // Error: Cannot access private property Example::$private_property
echo $example->get_private_property(); // Error: Call to private method Example::get_private_property() from context ''
echo $example->protected_property; // Error: Cannot access protected property Example::$protected_property
echo $example->get_protected_property(); // Error: Call to protected method Example::get_protected_property() from context ''

In summary, access modifiers in PHP OOP are used to restrict the visibility of properties and methods in a class. Public properties and methods can be accessed from outside the class, private properties and methods can only be accessed from within the class, and protected properties and methods can be accessed from within the class and its subclasses.