Menu
Error Identifier: method.private
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
class Foo
{
private function doFoo(): void
{
}
}
class Bar extends Foo
{
public function doBar(): void
{
$this->doFoo();
}
}
Why is it reported? #
This error is reported in two situations:
- A call is made to a private method of a parent class. Private methods are not accessible from child classes. In the example above,
Bartries to calldoFoo()which is a private method ofFoo. - A call is made to a private method on an object, and the calling scope does not have access to it.
Private visibility means the method can only be called from within the class where it is declared.
How to fix it #
If the method should be accessible to child classes, change the visibility to protected:
class Foo
{
- private function doFoo(): void
+ protected function doFoo(): void
{
}
}
If the method should be accessible from anywhere, change it to public:
class Foo
{
- private function doFoo(): void
+ public function doFoo(): void
{
}
}
How to ignore this error #
You can use the identifier method.private to ignore this error using a comment:
// @phpstan-ignore method.private
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: method.private