Search code examples
phpaccess-modifiers

Abstract methods visibility in PHP Object-oriented programming


I have an abstract class and I don't know the difference between the two ways of defining the test() function

abstract class Foo {
    abstract protected function test();
}

and this

abstract class Foo {
    abstract function test();
}

Does it make any difference?


Solution

  • From the PHP manual:

    Class methods may be defined as public, private, or protected. Methods declared without any explicit visibility keyword are defined as public.

    So the answer is no, they are not the same:

    • abstract protected function test(); will be accessible only within the class itself and by inheriting and parent classes;
    • abstract function test(); can be accessed from everywhere.