引用,指针和标识符之间有什么区别?

时间:2021-04-19 13:25:34

I was reading PHP manual link for OOPs concept and came across few examples which I was not able to understand.

我正在阅读OOPs概念的PHP手册链接,并且遇到了一些我无法理解的例子。

a) Can someone please explain whether the objects are getting passed by value or reference?

a)有人可以解释对象是通过值还是引用传递?

b) What is reference, pointer and identifier and what is difference between them?

b)什么是引用,指针和标识符,它们之间有什么区别?

Example 1:

class A {
    public $foo = 1;
}  

class B {
    public function foo(A $bar)
    {
        $bar->foo = 42;
    }

    public function bar(A $bar)
    {
        $bar = new A;
    }
}

$f = new A;
$g = new B;
echo $f->foo . "n";

$g->foo($f);
echo $f->foo . "n";

$g->bar($f);
echo $f->foo . "n";

Expected Output:

1
42
1

1 42 1

Returned Output:

1
42
42

1 42 42

Example 2:

class A
{
    public $v = 1;
}

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

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

$a = new A();

change($a);    
print_r($a); 
//Expected: A Object ( [v] => 2 ) 
//Returned: A Object ( [v] => 2 )  

makezero($a);    
print_r($a);
//Expected: 0 
//Returned: A Object ( [v] => 2 ) 

Reference 1

Reference 2

1 个解决方案

#1


0  

According to the php docs:

根据php文档:

By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference.

默认情况下,函数参数按值传递(因此,如果函数中的参数值发生更改,则不会在函数外部进行更改)。要允许函数修改其参数,必须通过引用传递它们。

To have an argument to a function always passed by reference, prepend an ampersand (&) to the argument name in the function definition.

要使函数的参数始终通过引用传递,请在函数定义中为参数名称添加一个与号(&)。

In B::foo you do not change the variable, but the property of the variable's object. That's why you get 42

在B :: foo中,您不会更改变量,而是更改变量对象的属性。这就是你得到42的原因

In B::bar you change the variable directly. It is changed to a new instance of A. Inside B::bar the value of $bar->foo === 1.

在B :: bar中,您可以直接更改变量。它被更改为A的新实例。在B :: bar中,$ bar-> foo === 1的值。

Pointers: Pointers are generated, whenever an object is created and assigned to a variable. This variable stores a memory address to access the object.

指针:每当创建对象并将其分配给变量时,都会生成指针。此变量存储用于访问对象的内存地址。

References: References are created with an ampersand (&).

参考:使用和号(&)创建参考。

//from your link posted
$a = new Foo; // $a is a pointer pointing to Foo object 0
$b = $a; // $b is a pointer pointing to Foo object 0, however, $b is a copy of $a
$c = &$a; // $c and $a are now references of a pointer pointing to Foo object 0

#1


0  

According to the php docs:

根据php文档:

By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference.

默认情况下,函数参数按值传递(因此,如果函数中的参数值发生更改,则不会在函数外部进行更改)。要允许函数修改其参数,必须通过引用传递它们。

To have an argument to a function always passed by reference, prepend an ampersand (&) to the argument name in the function definition.

要使函数的参数始终通过引用传递,请在函数定义中为参数名称添加一个与号(&)。

In B::foo you do not change the variable, but the property of the variable's object. That's why you get 42

在B :: foo中,您不会更改变量,而是更改变量对象的属性。这就是你得到42的原因

In B::bar you change the variable directly. It is changed to a new instance of A. Inside B::bar the value of $bar->foo === 1.

在B :: bar中,您可以直接更改变量。它被更改为A的新实例。在B :: bar中,$ bar-> foo === 1的值。

Pointers: Pointers are generated, whenever an object is created and assigned to a variable. This variable stores a memory address to access the object.

指针:每当创建对象并将其分配给变量时,都会生成指针。此变量存储用于访问对象的内存地址。

References: References are created with an ampersand (&).

参考:使用和号(&)创建参考。

//from your link posted
$a = new Foo; // $a is a pointer pointing to Foo object 0
$b = $a; // $b is a pointer pointing to Foo object 0, however, $b is a copy of $a
$c = &$a; // $c and $a are now references of a pointer pointing to Foo object 0