Heim  >  Artikel  >  Backend-Entwicklung  >  wordpress - PHP中$this和&$this有什么区别

wordpress - PHP中$this和&$this有什么区别

WBOY
WBOYOriginal
2016-06-06 20:41:011066Durchsuche

比如下面两个语句:

<code>call_user_func_array(array($this, 'func'), $param_arr);
call_user_func_array(array(&$this, 'func'), $param_arr);
</code>

其中$this和&$this,有什么区别吗?对象不都是引用嘛?在wordpress的钩子里经常见到类似下面的语句

<code>add_filter('import_post_meta_key', array($this, 'is_valid_meta_key'));
add_filter('http_request_timeout', array(&$this, 'bump_request_timeout'));
</code>

求解答~

回复内容:

比如下面两个语句:

<code>call_user_func_array(array($this, 'func'), $param_arr);
call_user_func_array(array(&$this, 'func'), $param_arr);
</code>

其中$this和&$this,有什么区别吗?对象不都是引用嘛?在wordpress的钩子里经常见到类似下面的语句

<code>add_filter('import_post_meta_key', array($this, 'is_valid_meta_key'));
add_filter('http_request_timeout', array(&$this, 'bump_request_timeout'));
</code>

求解答~

如果我没理解错的话,&$this 是为了兼容 PHP 5.2 及以下版本才这么写的,如果用了最新的 PHP 应该是等价的。

PHP引用传递,看下这篇文章就了解了 http://m.baidu.com/from=1269a/bd_page_type=1/ssid=0/uid=0/pu=usm%400%2Csz%401320_1001%2Cta%40iphone_2_4.1_3_537/baiduid=0AF2F0C34034DD7478809F025903D4FA/w=0_10_php%E5%BC%95%E7%94%A8%E4%BC%A0%E9%80%92/t=iphone/l=3/tc?ref=www_iphone&lid=8973481160499757776&order=1&vit=osres&tj=www_normal_1_0_10&m=8&srd=1&cltj=cloud_title&dict=20&fm=wnor&sec=43095&di=46cc7fb35bcae0a5&bdenc=1&nsrc=IlPT2AEptyoA_yixCFOxXnANedT62v3IEQGG_ytK1DK6mlrte4viZQRASDDmX7iNJVGwdoS4csxNaT0u3mRU7xJ_r0dmpWkbznuPdPrhgK3AHRMOfQNkNNbWVS7

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn