为什么PHP要求显式引用"$ this"?调用成员函数? [英] Why does PHP require an explicit reference to "$this" to call member functions?

查看:128
本文介绍了为什么PHP要求显式引用"$ this"?调用成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就是这样.大多数OO编程语言中的作用域可以解决符号问题,而无需显式引用当前实例(即PHP中的"$ this").为什么PHP要求我在每次调用同一个类中的成员函数之前都加上$ this?

That's pretty much it. Scoping in most OO programming languages resolves symbols fine without an explicit reference to the current instance (i.e. '$this' in PHP). Why does PHP require me to precede every call to a member function in the same class with $this?

推荐答案

解决函数调用的范围.考虑:

To resolve the scope of the function call. Consider:

$this->strstr(...

还有

strstr(...

后者将调用PHP的内置strstr()函数,这不是我们要在此处执行的操作.

The latter will call PHP's built-in strstr() function which is not what we want to do here.

这是在高级过程/脚本语言中添加OOP功能的结果.

It's a consequence of adding OOP features to a high level procedural/scripting language.

这篇关于为什么PHP要求显式引用"$ this"?调用成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆