PHP类与对象中的private访问控制的疑问
程序员文章站
2022-09-03 09:36:39
复制代码 代码如下: class bar { public function test() { $this->testprivate(); $this->tes...
复制代码 代码如下:
class bar
{
public function test() {
$this->testprivate();
$this->testpublic();
}
public function testpublic() {
echo "bar::testpublic\n";
}
private function testprivate() {
echo "bar::testprivate\n";
}
}
class foo extends bar
{
public function testpublic() {
echo "foo::testpublic\n";
}
private function testprivate() {
echo "foo::testprivate\n";
}
}
$myfoo = new foo();
$myfoo->test(); // bar::testprivate
// foo::testpublic
为啥第一行会输出bar::testprivate呢?
一些资料:
还有php官网上,关于这段代码的贡献者回复中,也找到了一条: