What are the differences between private, public, and protected visibility in PHP classes?

In PHP classes, visibility refers to the level of access that properties and methods have within the class and its subclasses. - Private visibility restricts access to only the class that defines the property or method. - Public visibility allows access from outside the class. - Protected visibility allows access only within the class and its subclasses. Here is an example demonstrating the differences between private, public, and protected visibility in PHP classes:

class Example {
    private $privateProperty = 'Private Property';
    public $publicProperty = 'Public Property';
    protected $protectedProperty = 'Protected Property';

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

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

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

$example = new Example();

// Accessing properties
echo $example->publicProperty; // Output: Public Property
// echo $example->privateProperty; // Error: Cannot access private property
// echo $example->protectedProperty; // Error: Cannot access protected property

// Accessing methods
$example->publicMethod(); // Output: Public Method
// $example->privateMethod(); // Error: Cannot access private method
// $example->protectedMethod(); // Error: Cannot access protected method