对"$this"的 PHP 引用

  • 本文关键字:quot PHP 引用 this php
  • 更新时间 :
  • 英文 :


PHP 如何解释&$this?为什么允许?

我带着以下问题来到这个问题,这看起来像 PHP 7.1 和 7.2 中的一个错误。 它发生在&$this引用和跨命名空间调用和call_user_func_array中。 我认为&$this很奇怪,不应该被允许,但WordPress使用它。

请考虑以下代码:

<?php
namespace N {
function callNeedRef( $a ) {
var_dump( $a );
call_user_func_array( 'needRef', $a );
}
}
namespace {
function needRef( &$r ) { } 
function callNeedRef( $a ) {
var_dump( $a );
call_user_func_array( 'needRef', $a );
}   
class C {
function f() {
$a = $this;
callNeedRef( array( &$a ) );        // no warning (expected), OK!
NcallNeedRef( array( &$a ) );      // no warning (expected), OK!
callNeedRef( array( &$this ) ); // no warning (expected), but 7.1,7.2: var_dump prints no '&'           
NcallNeedRef( array( &$this ) );   // 7.1,7.2: warn and var_dump prints no '&'         
}
}
echo "<pre>";
echo phpversion() . PHP_EOL;
$o = new C();
$o->f();
}

及其输出:

7.2.0RC2
array(1) {
[0]=>
&object(C)#1 (0) {
}
}
array(1) {
[0]=>
&object(C)#1 (0) {
}
}
array(1) {
[0]=>
object(C)#1 (0) {
}
}
array(1) {
[0]=>
object(C)#1 (0) {
}
}

Warning:  Parameter 1 to needRef() expected to be a reference, value given in reftest.php on line 6

如代码中所述,最后两个var_dump不会将对象标记为引用。最后一个呼叫甚至会产生警告。

val ($this) 不是全局的,范围会发生变化,因此它会ZCAL_STR_COPYIED。

case EXTR_OVERWRITE:
/* GLOBALS protection */
if (var_exists && ZSTR_LEN(var_name) == sizeof("GLOBALS")-1 && !strcmp(ZSTR_VAL(var_name), "GLOBALS")) {
break;
}
if (var_exists && ZSTR_LEN(var_name) == sizeof("this")-1  && !strcmp(ZSTR_VAL(var_name), "this")) {
zend_class_entry *scope = zend_get_executed_scope();
if (scope && ZSTR_LEN(scope->name) != 0) {
break;
}
}
ZVAL_STR_COPY(&final_name, var_name);
break;

谢谢你让我研究array.c的PHP源代码:)

最新更新