What are the differences between the two ways that a PHP object is assigned to a variable, a general assignment and a reference assignment?

Source: Internet
Author: User
Class Person {
Public $name;
protected $age;
Public function __construct ($name, $age) {
$this->name = $name;
$this->age = $age;
}
}

$LH = new Person (' Emily Lau ', 23);
$a = $LH;
$b =& $lh;
-------------------------------------------------------------------------
What is the difference between $ A and $b? Can be analyzed from the perspective of the memory stack.

··

Reply content:

There is no need to add & quote symbols in PHP. All objects are reference passes, unless you explicitly call clone $object laxatives
===============
I forgot which version to start with, and I don't need to add & quote symbols in PHP.
Add the will report a grammatical error, this detailed answer, you can ask @ Hantian (he also answered).
Or ask @Laruence Uncle Bird the Danale.
Assigning a value to a variable of a basic type of sauce, usually a true copy (understood as: 2 pointers, pointing to 2 memory spaces).
$a = 100;$b = $a;unset($a);echo $b."\n";
There are some differences, in your case, if you assign a new value to B, the direction of LH will also change, but a will not have this problem.

The reference in the PHP manual is:

Note:

$a and $b is completely equal here. $a is not pointing to $b or vice versa. $a and $b is pointing to the same place.


The arguments to objects and references in PHP5 are:


A PHP Reference is an alias, which allows and different variables to write to the same value. As of PHP 5, an object variable doesn ' t contain the object itself as value anymore. It is only contains a object identifier which allows object accessors to find the actual object. When an object was sent by argument, returned or assigned to another variable, the different variables was not aliases:the Y hold a copy of the identifier, which points to the same object.


In fact, the object variable take & more like a pointer to the pointer.

@ Li Huaizhi I also remember that PHP does not need to add & quote symbols, are reference delivery. But after my test (PHP5.6.7), I was puzzled by the following:
class Person {  public $name = 'myName';}$obj = new Person;$assign = $obj;$reference = & $obj;$obj = null;//此操作很重要var_dump($obj);//得到nullvar_dump($assign);//得到object(Person)#1 (1) {["name"]=>string(6) "myName"}var_dump($reference);//得到null
Baidu a bit exactly the same
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.