PHP OOP - Access Modifiers

In PHP, access modifiers are keywords that define the visibility and accessibility of class properties and methods. They control how these members can be accessed and manipulated from outside the class. There are three main access modifiers in PHP:


1. Public (`public`):

- When a property or method is declared as `public`, it can be accessed from anywhere, both inside and outside the class.
- It is the least restrictive access modifier, and the class members marked as `public` are accessible to all code in the program.

php
class MyClass {
    public $publicProperty;

    public function publicMethod() {
        // Code here...
    }
}

2. Protected (`protected`):

- When a property or method is declared as `protected`, it can be accessed from within the class itself and its subclasses (classes that inherit from this class).
- It allows for better encapsulation by restricting direct access from outside the class.

php
class MyClass {
    protected $protectedProperty;

    protected function protectedMethod() {
        // Code here...
    }
}

3. Private (`private`):

- When a property or method is declared as `private`, it can only be accessed from within the class where it is defined. It is not accessible from subclasses or any other part of the code.
- It provides the highest level of encapsulation and is often used to protect sensitive data.

php
class MyClass {
    private $privateProperty;

    private function privateMethod() {
        // Code here...
    }
}

Here's an example demonstrating the use of access modifiers:

php
class Example {
    public $publicVar = 'Public'; // Accessible from anywhere
    protected $protectedVar = 'Protected'; // Accessible within the class and its subclasses
    private $privateVar = 'Private'; // Accessible only within the class itself

    public function publicMethod() {
        echo $this->publicVar . "\n";
        echo $this->protectedVar . "\n";
        echo $this->privateVar . "\n";
    }

    protected function protectedMethod() {
        // Code here...
    }

    private function privateMethod() {
        // Code here...
    }
}
class SubExample extends Example {
    public function accessProtected() {
        echo $this->protectedVar . "\n";
        // echo $this->privateVar; // Error: Cannot access private property
    }
}

$object = new Example();
$object->publicMethod(); // Output: Public \n Protected \n Private \n

$subObject = new SubExample();
$subObject->accessProtected(); // Output: Protected \n

Remember that proper usage of access modifiers helps maintain the integrity of your classes and promotes good OOP principles such as encapsulation and data hiding.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext