“&"是什么意思?符号在PHP中意味着什么? [英] What does the "&" sign mean in PHP?

查看:295
本文介绍了“&"是什么意思?符号在PHP中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Google上找到此答案,但是我想符号&可以作为某种运算符使用,或者由于某种原因而通常不是可搜索的术语.在学习如何创建WordPress插件时,我看到了此代码段,因此,我只需要知道&在包含类对象的变量之前的含义.

I was trying to find this answer on Google, but I guess the symbol & works as some operator, or is just not generally a searchable term for any reason.. anyhow. I saw this code snippet while learning how to create WordPress plugins, so I just need to know what the & means when it precedes a variable that holds a class object.

//Actions and Filters
if (isset($dl_pluginSeries)) {

    //Actions
    add_action('wp_head', array(&$dl_pluginSeries, 'addHeaderCode'), 1);
    //Filters
    add_filter('the_content', array(&$dl_pluginSeries, 'addContent'));
}

推荐答案

这将强制变量通过引用传递.通常,将为简单类型创建一个硬拷贝.对于较大的字符串(提高性能),或者如果您想在不使用return语句的情况下操作变量,这可能会派上用场,例如:

This will force the variable to be passed by reference. Normally, a hard copy would be created for simple types. This can come handy for large strings (performance gain) or if you want to manipulate the variable without using the return statement, eg:

$a = 1;

function inc(&$input)
{
   $input++;
}

inc($a);

echo $a; // 2

对象将自动通过引用传递.

Objects will be passed by reference automatically.

如果您想将副本复制到函数中,请使用

If you like to handle a copy over to a function, use

clone $object;

然后,原始对象未更改,例如:

Then, the original object is not altered, eg:

$a = new Obj;
$a->prop = 1;
$b = clone $a;
$b->prop = 2; // $a->prop remains at 1

这篇关于“&"是什么意思?符号在PHP中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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