website/errors/staticMethod.private.md
<?php declare(strict_types = 1);
class Foo
{
private static function computeSecret(): int
{
return 42;
}
}
class Bar
{
public function doFoo(): int
{
return Foo::computeSecret();
}
}
The code calls a private static method from a class that is not the declaring class. Private methods are only accessible from within the class that defines them. They are not accessible from subclasses or any other class. This results in a fatal error at runtime.
If the method needs to be called from outside the class, change its visibility to public or protected:
<?php declare(strict_types = 1);
class Foo
{
- private static function computeSecret(): int
+ public static function computeSecret(): int
{
return 42;
}
}
Alternatively, expose the functionality through a public method:
<?php declare(strict_types = 1);
class Foo
{
private static function computeSecret(): int
{
return 42;
}
+
+ public static function getResult(): int
+ {
+ return self::computeSecret();
+ }
}
class Bar
{
public function doFoo(): int
{
- return Foo::computeSecret();
+ return Foo::getResult();
}
}