PHP中的& $变量 [英] &$variable in PHP

查看:75
本文介绍了PHP中的& $变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究wordpress的核心文件,偶然发现了这段代码,我注意到它在变量名前和后有一个&符号。

I have been looking through wordpress's core files and stumbled across this piece of code, I noticed it had an ampersand before a variable name and after an =.

我尝试过搜索并找到这个来自PHP手册,并没有很好的解释,或者我看错了!我也看到它用于修改使用它的方法之外的一个变量,但是,那是一个变量,需要修改,所以如果这是正确的,那么将如何使用它?

I have tried searching this and came across this from the PHP manual and it doesn't explain it well, or I'm looking at the wrong one! I also saw that it is used to modify a variable outside of the method where it is being used, but, thats what a variable is there for, to be modified so if this is correct how would one use it?

function _make_cat_compat( &$category ) {

    if ( is_object( $category ) ) {
        $category->cat_ID = &$category->term_id;
        $category->category_count = &$category->count;
        $category->category_description = &$category->description;
        $category->cat_name = &$category->name;
        $category->category_nicename = &$category->slug;
        $category->category_parent = &$category->parent;
    } elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
        $category['cat_ID'] = &$category['term_id'];
        $category['category_count'] = &$category['count'];
        $category['category_description'] = &$category['description'];
        $category['cat_name'] = &$category['name'];
        $category['category_nicename'] = &$category['slug'];
        $category['category_parent'] = &$category['parent'];
    }
}


推荐答案

意味着函数将修改参数(通过引用),而不是处理它的副本。

This means the function will modify the argument (by reference) instead working on a copy of it. Remove all the ampersands inside the body of the function, only the one in the argument is necessary.

function foo(&$foo) { $foo++; }
function bar($foo) { $foo++; }
$foo = 10;
foo($foo);
foo($foo);

// prints 12, function foo() has incremented var $foo twice
echo "$foo\n";

bar($foo);
// still 12, as bar() is working on a copy of $foo
echo "$foo\n";


// However, since PHP 5.0, all objects are passed by reference [(or to be more specific, by identifier)][1]
class Foo {
    public $bar = 10;
}
$obj = new Foo;
echo "$obj->bar\n"; // 10, as expected

function objectIncrement($obj) { $obj->bar++; }

function objectRefIncrement(&$obj) { $obj->bar++; }

objectIncrement($obj);
echo "$obj->bar\n"; // 11, as expected, since objects are ALWAYS passed by reference (actually by identifier)
objectRefIncrement($obj);
echo "$obj->bar\n"; // 12

如果您打算修改传入的参数,它仍然是一个好主意一个函数/方法,明确地通过引用来传递它。除了其他优点,你的代码也变得更加明确和易于理解。

It's still a good idea, if you intend to modify the passed argument in a function/method, to explicitly pass it by reference. Aside from other advantages, your code also becomes more explicit and understandable.

顺便说一句,你可以做这:

BTW, you can do this:

function _make_cat_compat( &$category ) {
    if (is_array( $category)) {
        $category = (object)$category;
    }

    $category->cat_ID = $category->term_id;
    $category->category_count = $category->count;
    $category->category_description = $category->description;
    $category->cat_name = $category->name;
    $category->category_nicename = $category->slug;
    $category->category_parent = $category->parent;
}

看起来更清洁,但我不知道您的具体情况。而且我不知道你会如何使用数组或对象 - 这意味着使用了一些不好的做法。

Looks cleaner to me, but I don't know your specific case. And I don't know how you would have either array or object - it implies some bad practices used.

这篇关于PHP中的& $变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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