在 PHP 5 中如何通过引用传递对象?

发布于 2025-01-06 06:03:26 字数 390 浏览 0 评论 0原文

在 PHP 5 中,是否需要使用 & 修饰符来按引用传递?例如,

class People() { }
$p = new People();
function one($a) { $a = null; }
function two(&$a) { $a = null; )

在 PHP4 中,您需要 & 修饰符在进行更改后维护引用,但我对我读过的有关 PHP5 自动使用引用传递的主题感到困惑,除非显式克隆对象。

在 PHP5 中,是否需要 & 修饰符来通过引用传递所有类型的对象(变量、类、数组……)?

In PHP 5, are you required to use the & modifier to pass by reference? For example,

class People() { }
$p = new People();
function one($a) { $a = null; }
function two(&$a) { $a = null; )

In PHP4 you needed the & modifier to maintain reference after a change had been made, but I'm confused on the topics I have read regarding PHP5's automatic use of pass-by-reference, except when explicity cloning the object.

In PHP5, is the & modifier required to pass by reference for all types of objects (variables, classes, arrays, ...)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帥小哥 2025-01-13 06:03:26

您是否需要使用&修饰符来传递引用?

从技术/语义上讲,答案是,即使对于对象也是如此。这是因为有两种方法可以传递/分配对象:通过引用或通过标识符。当函数声明包含 & 时,如下所示:

function func(&$obj) {}

无论如何,参数都将通过引用传递。如果您在没有 & 的情况下进行声明,则

function func($obj) {}

所有内容都将按值传递,但对象和资源除外,它们将通过标识符传递。什么是标识符?嗯,你可以把它看作是对参考的参考。举个例子:

class A
{
    public $v = 1;
}

function change($obj)
{
    $obj->v = 2;
}

function makezero($obj)
{
    $obj = 0;
}

$a = new A();

change($a);

var_dump($a); 

/* 
output:

object(A)#1 (1) {
  ["v"]=>
  int(2)
}

*/

makezero($a);

var_dump($a);

/* 
output (same as before):

object(A)#1 (1) {
  ["v"]=>
  int(2)
}

*/

那么为什么$a在传递给makezero之后没有突然变成一个整数呢?这是因为我们只覆盖了标识符。如果我们通过引用传递:

function makezero(&$obj)
{
    $obj = 0;
}

makezero($a);

var_dump($a);

/* 
output:

int(0) 

*/

现在$a是一个整数。因此,通过标识符传递和通过引用传递之间存在差异。

are you required to use the & modifier to pass-by-reference?

Technically/semantically, the answer is yes, even with objects. This is because there are two ways to pass/assign an object: by reference or by identifier. When a function declaration contains an &, as in:

function func(&$obj) {}

The argument will be passed by reference, no matter what. If you declare without the &

function func($obj) {}

Everything will be passed by value, with the exception of objects and resources, which will then be passed via identifier. What's an identifier? Well, you can think of it as a reference to a reference. Take the following example:

class A
{
    public $v = 1;
}

function change($obj)
{
    $obj->v = 2;
}

function makezero($obj)
{
    $obj = 0;
}

$a = new A();

change($a);

var_dump($a); 

/* 
output:

object(A)#1 (1) {
  ["v"]=>
  int(2)
}

*/

makezero($a);

var_dump($a);

/* 
output (same as before):

object(A)#1 (1) {
  ["v"]=>
  int(2)
}

*/

So why doesn't $a suddenly become an integer after passing it to makezero? It's because we only overwrote the identifier. If we had passed by reference:

function makezero(&$obj)
{
    $obj = 0;
}

makezero($a);

var_dump($a);

/* 
output:

int(0) 

*/

Now $a is an integer. So, there is a difference between passing via identifier and passing via reference.

幽蝶幻影 2025-01-13 06:03:26

对象将按引用传递。内置类型将按值传递(复制);

幕后发生的事情是,当您传入保存对象的变量时,它是对该对象的引用。因此变量本身被复制,但它仍然引用同一个对象。因此,本质上有两个变量,但两者都指向同一个对象。对函数内对象所做的更改将持续存在。

对于您那里的代码(首先您需要 $ 即使带有 &):

$original = new Object();

one($original); //$original unaffected
two($original); //$original will now be null

function one($a) { $a = null; } //This one has no impact on your original variable, it will still point to the object

function two(&$a) { $a = null; ) //This one will set your original variable to null, you'll lose the reference to the object.

Objects will pass-by-reference. Built in types will be pass-by-value (copied);

What is happening behind the scenes is that when you pass in a variable that holds an object, it's a reference to the object. So the variable itself is copied, but it still references the same object. So, essentially there are two variable, but both are pointing to the same object. Changes made to objects inside a function will persist.

In the case of the code that you have there (first you need $ even with &):

$original = new Object();

one($original); //$original unaffected
two($original); //$original will now be null

function one($a) { $a = null; } //This one has no impact on your original variable, it will still point to the object

function two(&$a) { $a = null; ) //This one will set your original variable to null, you'll lose the reference to the object.
对你而言 2025-01-13 06:03:26

你用错了。 $ 符号对于任何变量都是必需的。应该是:
http://php.net/manual/en/language.references.pass.php

function foo(&$a)
{
$a=null;
}


foo($a);
To return a reference, use

 function &bar($a){
$a=5;
return $a

 }

在对象和数组中,对象的引用被复制为形式参数,两个对象上的任何相等操作都是引用交换。

$a=new People();
$b=$a;//equivalent to &$b=&$a roughly. That is the address of $b is the same as that of $a 

function goo($obj){
//$obj=$e(below) which essentially passes a reference of $e to $obj. For a basic datatype such as string, integer, bool, this would copy the value, but since equality between objects is anyways by references, this results in $obj as a reference to $e
}
$e=new People();
goo($e);

You're using it wrong. The $ sign is compulsory for any variable. It should be:
http://php.net/manual/en/language.references.pass.php

function foo(&$a)
{
$a=null;
}


foo($a);
To return a reference, use

 function &bar($a){
$a=5;
return $a

 }

In objects and arrays, a reference to the object is copied as the formal parameter, any equality operations on two objects is a reference exchange.

$a=new People();
$b=$a;//equivalent to &$b=&$a roughly. That is the address of $b is the same as that of $a 

function goo($obj){
//$obj=$e(below) which essentially passes a reference of $e to $obj. For a basic datatype such as string, integer, bool, this would copy the value, but since equality between objects is anyways by references, this results in $obj as a reference to $e
}
$e=new People();
goo($e);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文