发布于2023-11-29 20:02 阅读(416) 评论(0) 点赞(6) 收藏(3)
<?php
class foo
{
private $callVar = false;
private $constructorVar = false;
public function __construct()
{
$this->constructorVar = get_class($this);
}
public function __call($methodName, $arguments)
{
$this->callVar = get_class($this);
call_user_func_array(array($this, $methodName), $arguments);
}
protected function method($params)
{
echo('<br>Call var: '.$this->callVar.'<br>Constructor var: '.$this->constructorVar);
}
}
class foo_sec extends foo
{
protected function method($params)
{
echo 'First call: ';
parent::method($params);
echo '<br> Second call: ';
$test = new foo_trd();
$test->method('param2');
}
}
class foo_trd extends foo
{
}
$m = new foo_sec();
$m->method('param');
?>
给出这个结果:
First call:
Call var: foo_sec
Constructor var: foo_sec
Second call:
Call var:
Constructor var: foo_trd
此代码应返回当前正在运行的实例的名称,但如果该实例位于另一个实例内,则第二个实例中的类名称为空。这是 PHP 错误还是我遗漏的其他问题?
换句话说,Call var
第二次调用中的 应该是foo_trd
- 不为空。
我相信您正在经历一些与 PHP 及其继承可见性相关的有趣事情。
简而言之,当您在 和 的实例内部时,Foo
您可以访问也扩展的所有其他实例的protected
和范围。由于您可以直接访问 ,因此您将跳过对 的调用。private
Foo
protected method()
__call()
当您想要将构造函数设为私有并强制执行工厂时,这实际上很有用。
作者:黑洞官方问答小能手
链接:http://www.phpheidong.com/blog/article/550172/e21f941989842f46a217/
来源:php黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 php黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-4
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!