website/errors/property.protected.md
<?php declare(strict_types = 1);
class Foo
{
protected string $bar = 'hello';
}
function doFoo(Foo $foo): void
{
echo $foo->bar;
}
The property $bar is declared as protected, which means it can only be accessed from within the class itself or from its subclasses. Accessing it from outside the class hierarchy -- such as from a standalone function or from an unrelated class -- violates the visibility constraint and would cause a fatal error at runtime.
Add a public getter method to expose the property value:
<?php declare(strict_types = 1);
class Foo
{
protected string $bar = 'hello';
+
+ public function getBar(): string
+ {
+ return $this->bar;
+ }
}
function doFoo(Foo $foo): void
{
- echo $foo->bar;
+ echo $foo->getBar();
}
Or change the property's visibility to public if it is safe to do so:
<?php declare(strict_types = 1);
class Foo
{
- protected string $bar = 'hello';
+ public string $bar = 'hello';
}
Or move the accessing code into the class or a subclass where the protected property is accessible.