Method and Property Visibility

suggest change

There are three visibility types that you can apply to methods (class/object functions) and properties (class/object variables) within a class, which provide access control for the method or property to which they are applied.

You can read extensively about these in the PHP Documentation for OOP Visibility.

Public

Declaring a method or a property as public allows the method or property to be accessed by:

An example of this public access would be:

class MyClass {
    // Property
    public $myProperty = 'test';

    // Method
    public function myMethod() {
        return $this->myProperty;
    }
}

$obj = new MyClass();
echo $obj->myMethod();
// Out: test

echo $obj->myProperty;
// Out: test

Protected

Declaring a method or a property as protected allows the method or property to be accessed by:

This does not allow external objects, classes, or code outside the class hierarchy to access these methods or properties. If something using this method/property does not have access to it, it will not be available, and an error will be thrown. Only instances of the declared self (or subclasses thereof) have access to it.

An example of this protected access would be:

class MyClass {
    protected $myProperty = 'test';

    protected function myMethod() {
        return $this->myProperty;
    }
}

class MySubClass extends MyClass {
    public function run() {
        echo $this->myMethod();
    }
}

$obj = new MySubClass();
$obj->run(); // This will call MyClass::myMethod();
// Out: test

$obj->myMethod(); // This will fail.
// Out: Fatal error: Uncaught Error: Call to protected method MyClass::myMethod() from global scope

A protected member can be used only from the declaring class and its subclasses. Code outside that hierarchy cannot call it.


Private

Declaring a method or a property as private allows the method or property to be accessed by:

A private method or property is only visible and accessible within the class that created it.

Another instance of the same class can read and write that instance's private and protected members. The access check is on the class, not on the object identity.

class MyClass {
    private $myProperty = 'test';

    private function myPrivateMethod() {
        return $this->myProperty;
    }

    public function myPublicMethod() {
        return $this->myPrivateMethod();
    }

    public function modifyPrivatePropertyOf(MyClass $anotherInstance) {
        $anotherInstance->myProperty = "new value";
    }
}

class MySubClass extends MyClass {
    public function run() {
        echo $this->myPublicMethod();
    }

    public function runWithPrivate() {
        echo $this->myPrivateMethod();
    }
}

$obj = new MySubClass();
$newObj = new MySubClass();

// This will call MyClass::myPublicMethod(), which will then call
// MyClass::myPrivateMethod();
$obj->run(); 
// Out: test

    
$obj->modifyPrivatePropertyOf($newObj);

$newObj->run();
// Out: new value

echo $obj->myPrivateMethod(); // This will fail.
// Out: Fatal error: Uncaught Error: Call to private method MyClass::myPrivateMethod() from global scope

echo $obj->runWithPrivate(); // This will also fail.
// Out: Fatal error: Uncaught Error: Call to private method MyClass::myPrivateMethod() from scope MySubClass

A private method or property is visible only inside the class that declares it.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents