I have a PHP script which works fine in PHP 5, but not in PHP 4. I've made a small test case for you to demonstrate (disclaimer: I know that the below code could be written much better, but it's not an actually used piece, rather the one to demonstrate what I'm talking about):
我有一个PHP脚本可以在PHP 5中正常工作,但在PHP 4中没有。我已经为你做了一个小的测试用例(免责声明:我知道下面的代码可以编写得更好,但它实际上不是用过的作品,而不是用来展示我在说什么的作品:
class Messenger {
var $messages = '';
function add($message) {
$this->messages .= "$message\n";
}
}
function add($m) {
if (! isset($GLOBALS['instance'])) $GLOBALS['instance'] = new Messenger();
call_user_func_array(array($GLOBALS['instance'], 'add'), array($m));
}
add("One");
add("Two");
add("Three");
var_dump($GLOBALS['instance']->messages);
Under PHP 5 the messages
property contains all 3 messages, under PHP 4 it is empty. Why?
在PHP 5下,messages属性包含所有3条消息,在PHP 4下它是空的。为什么?
1 个解决方案
#1
3
In PHP 4, $this
does not seems to be work the same way as PHP 5 does.
在PHP 4中,$ this似乎与PHP 5的工作方式不同。
The $this pseudo-variable is not usually defined if the method in which it is hosted is called statically. This is not, however, a strict rule: $this is defined if a method is called statically from within another object. In this case, the value of $this is that of the calling object. This is illustrated in the following example:
如果托管它的方法是静态调用的,则通常不定义$ this伪变量。但是,这不是一个严格的规则:如果从另一个对象内静态调用一个方法,则定义$ this。在这种情况下,$ this的值是调用对象的值。以下示例说明了这一点:
example : http://www.php.net/manual/en/keyword.class.php
例如:http://www.php.net/manual/en/keyword.class.php
#1
3
In PHP 4, $this
does not seems to be work the same way as PHP 5 does.
在PHP 4中,$ this似乎与PHP 5的工作方式不同。
The $this pseudo-variable is not usually defined if the method in which it is hosted is called statically. This is not, however, a strict rule: $this is defined if a method is called statically from within another object. In this case, the value of $this is that of the calling object. This is illustrated in the following example:
如果托管它的方法是静态调用的,则通常不定义$ this伪变量。但是,这不是一个严格的规则:如果从另一个对象内静态调用一个方法,则定义$ this。在这种情况下,$ this的值是调用对象的值。以下示例说明了这一点:
example : http://www.php.net/manual/en/keyword.class.php
例如:http://www.php.net/manual/en/keyword.class.php