[Solved] php oop protected property, protected method, protected construct


This is the purpose of Visibility.

In Block 2 your property is protected. This means it can only be accessed the class itself (Database2) and by inherited classes. The error occurs when you try to echo the variable from the outside.

The same goes for the method in Block 3.

The Constuctor can be protected or even private. But you cannot call it from the outside anymore. But something like this is possible:

class Foo
{
    private function __construct()
    {
    }

    public static function create()
    {
        return new self();
    }
}

$foo = Foo::create();

solved php oop protected property, protected method, protected construct