PHP使用语言结构与魔术方法相结合
发布时间:2020-12-13 14:00:32 所属栏目:PHP教程 来源:网络整理
导读:这个 question让我很好奇地使用语言结构与PHP的魔法方法相结合.我已经创建了一个演示代码: ?phpclass Testing { public function scopeList() { echo "scopeList"; } public function __call($method,$parameters) { if($method == "list") { $this-scopeLi
这个
question让我很好奇地使用语言结构与PHP的魔法方法相结合.我已经创建了一个演示代码:
<?php class Testing { public function scopeList() { echo "scopeList"; } public function __call($method,$parameters) { if($method == "list") { $this->scopeList(); } } public static function __callStatic($method,$parameters) { $instance = new static; call_user_func_array([$instance,$method],$parameters); } } //Testing::list(); $testing = new Testing(); $testing->list(); 为什么Testing :: list()抛出一个语法错误,而$test-> list()没有? 由于php reserved keywords都应该失败?
更新PHP 7
PHP 7解决了所描述的行为,并实现了一个名为上下文敏感词典的功能,由marcio提出. 您的代码将简单地使用PHP 7. PHP 7之前的情况 在PHP甚至知道通过__callStatic()可用的方法的事实之前,会抛出语法错误,它发生在解析阶段. 您描述的行为似乎是PHP解析器中的一个错误,至少应该在文档中描述的不一致. 我会file a bug report.好抓住! 更新:OP已经提供了一个错误报告,可以在这里找到:https://bugs.php.net/bug.php?id=71157 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |