Variable assignment versus object assignmentCopy the code

       
    // Declare a variable and assign a value
    $a = 1; 
    // Assign the value of the data type to a variable
    $b = $a; 

    // Change the value of $a
    $a = 2; 

    $a and $b are two independent memory space changes, one of which is unaffected by the other
    echo $b; / / 1

    class Person{
        public $name;
        public $age;

    }

    // Assign data of the object type to a variable
    $p = new Person;

    // By modifying the value of the attribute, we can determine the way the object is passed in object-oriented
    $p->name = 'jesse';

    // If the value is successfully modified, the value is passed by reference
    echo $p->name; // jesse

? >
Copy the code

Case Explanation:

In this case, the method of assigning a variable is compared with the method of assigning an object
b = b =
a; The assignment mode of is “copy assignment”, so when modifying its source value (
a The value of the ) In the case of, The value of a),
The value of B is not affected.
a with A and
The memory diagram of B is as follows:The code
p > n a m e = j e s s e ; Assigns the data type to p->name = ‘jesse’; Assigns the data type to
$p = $p = $p = $p = $p = $p = $p


       
    class Person
    {
        public $name;
        public $age;

        // Assign data of the object type to a variable
        $p = new Person;

        // By modifying the value of the attribute, we can determine the way the object is passed in object-oriented
        $p->name = 'jesse';

        // Assign the object to another variable
        $m = $p;

        // Another variable modifies the attribute value
        $m->name = 'Marry';

        // Output the result of the source variable assignment variable accessing the internal attribute
        echo $p->name, $m->name;
    }

? >
Copy the code


m = m =
p; $m is a copy of the object. $m is a copy of the object. Or do you pass by reference? The code memory relationship is as follows:Therefore, the last two objects’ property values are changed to Marry!!

So what happens when you pass by reference?


       
    class Person
    {
        public $name; 
        public $age; 
    } 

    // Assign data of the object type to a variable
    $p = new Person; 

    // By modifying the value of the attribute, we can determine the way the object is passed in object-oriented
    $p->name = 'jesse';

    // Assign the object to another variable
    $m = &$p;

    // Another variable modifies the attribute value
    $m->name = 'Marry';

    // Output the result of the source variable assignment variable accessing the internal attribute
    echo $p->name, $m->name;

? >
Copy the code

The above content hopes to help you, more PHP factory PDF, PHP advanced architecture video materials, PHP wonderful good article can be searched on wechat: PHP open source community

2021 Jinsanyin four big factory interview real questions collection, must see!

Four years of PHP technical articles collation collection – PHP framework

A collection of four years’ worth of PHP technical articles – Microservices Architecture

Distributed Architecture is a four-year collection of PHP technical articles

Four years of PHP technical essays – High Concurrency scenarios

Four years of elite PHP technical article collation collection – database