PHP inheritance and protected member visibility

后端 未结 2 721
死守一世寂寞
死守一世寂寞 2021-02-13 11:28

I\'ve found something that appears to be a strange inheritance issue in PHP.

From the PHP manual:

Members declared protected can be accessed only

相关标签:
2条回答
  • 2021-02-13 12:03

    There is no rationale about this, it has been reported 2 years ago: https://bugs.php.net/bug.php?id=52120

    0 讨论(0)
  • 2021-02-13 12:05

    You can access those methods because there is a declaration of them as protected in Foo, which is your parent and that gives you permission to access it. If you remove the declaration from the parent and declare the protected method in B you will get a Fatal Error.

    This is reported as a bug in PHP https://bugs.php.net/bug.php?id=50892

    0 讨论(0)
提交回复
热议问题