How to Work with Visibility (Public, Private, Protected) in PHP

Updated: January 10, 2024 By: Guest Contributor Post a comment

Overview

Understanding object visibility is crucial for proper object-oriented programming. In PHP, visibility refers to where and how properties and methods are accessible within your classes. This tutorial will guide you through the use of public, private, and protected keywords, which determine the level of access control in PHP classes. You’ll learn the basics of encapsulation, dive into each type of visibility, and see practical code examples demonstrating how visibility affects your PHP code.

Public Visibility

Public visibility means that the property or method can be accessed from anywhere – both within and outside of the class. This is the default visibility for class members in PHP if you don’t explicitly set one.

Here’s a basic example of a public property and method:

<?php
class Car {
    public $color = 'red';

    public function getColor() {
        return $this->color;
    }
}

$myCar = new Car();
echo $myCar->getColor(); // Outputs: red
?>

This simple class has a public property $color and a public method getColor(). Both can be accessed from outside the class, as seen with the instantiation of the Car object and invoking the getColor() method.

Private Visibility

Private visibility restricts access to the property or method within the class that defines it. This means that you cannot access private members directly from outside the class. This is useful for hiding internal state and helper methods that should not be part of the object’s public interface.

Here’s an example of private visibility in action:

<?php
class Car {
    private $color = 'red';

    private function setColor($color) {
        $this->color = $color;
    }

    public function getColor() {
        return $this->color;
    }
}

$myCar = new Car();
echo $myCar->getColor(); // Outputs: red
//$myCar->setColor('blue'); // Fatal error: Uncaught Error: Call to private method
?>

In the updated Car class, the $color property and the setColor() method are now private. The public method getColor() is still accessible from outside the class, but you cannot access or change the color property directly.

Protected Visibility

Protected visibility is a level between public and private. Properties and methods marked as protected can be accessed within the class itself, by inheriting classes, but not from the outside world.

Here’s how you might use protected visibility to allow a child class to access its parent’s properties or methods:

<?php
class Vehicle {
    protected $color;

    public function setColor($color) {
        $this->color = $color;
    }

    protected function getColor() {
        return $this->color;
    }
}

class Car extends Vehicle {
    public function revealColor() {
        return $this->getColor();
    }
}

$myCar = new Car();
$myCar->setColor('blue');
echo $myCar->revealColor(); // Outputs: blue
?>

In the Vehicle class, $color is protected and cannot be accessed directly from an instance of Car. However, because Car is a subclass of Vehicle, it can access the protected method getColor() within its own public method revealColor().

Advanced Use Cases

As you become more comfortable with visibility in PHP, you can apply these principles in more advanced scenarios, such as within design patterns and frameworks. Here are a couple of examples:

<?php
// ... 

use some design pattern;

class AdvancedExample extends SomePattern {
    // Implementation details
}

// ...
?>

Advanced use cases often involve protected visibility within abstract classes and interfaces that require strictly defined APIs. These examples exceed the beginner level and enter the realm of intermediate and advanced PHP programming.

Conclusion

In conclusion, visibilities in PHP provide you with the tools to protect the integrity of your objects by controlling how their state and behavior are accessed. Through public, private, and protected keywords, you can build a robust and secure object-oriented application. Remember to carefully consider which visibility is appropriate for each method and property in your classes, keeping in mind the principles of encapsulation and inheritance.