Broad Network


References in Objects and Arrays

PHP References - Part 3

Foreword: In this part of the series, I talk about References in Objects and Arrays.

By: Chrysanthus Date Published: 2 Dec 2018

Introduction

This is part 3 of my series, PHP Reference. In this part of the series, I talk about References in Objects and Arrays. You should have read the previous parts of the series before coming here, as this is the continuation.

Reference as a Property
A reference can be the property of a class. Try the following code:

<?php

    $vr = 'I love you.';

    class Cla
        {
            public $prop;

            function __construct()
                {
                    global $vr;
                    $this->prop = &$vr;
                }
        }

    $obj = new Cla();
    $vr = 'I hate you.';
    echo $obj->prop;

?>

In the creation of the object, the construction assigns a reference to the property. Outside and below the class definition, the content of the reference is changed. The output is:

    I hate you.

Copying an Object
When copying an object to a variable, the references to the object properties are not copied - they cannot be copied and should not be copied. The official syntax to copy an object in PHP is:

    $copy_of_object = clone $object;

Try the following code, which shows that the reference to an object property has not been copied:

<?php

    $vr = 'I love you.';

    class Cla
        {
            public $prop;

            function __construct()
                {
                    global $vr;
                    $this->prop = &$vr;
                }
        }

    $obj = new Cla();
    echo $obj->prop, '<br>';
    $vr = 'I hate you.';
    echo $obj->prop, '<br>';
    $obj2 = clone $obj;
    $vr = 'Why?';
    echo $obj2->prop;

?>

The output is:

    I love you.
    I hate you.
    Why?

Reference as Value of Element in Array
A reference can be the value of an element in an array. Try the following code:

<?php

    $vr = "yellow";

    $arr = array('Apple' => "purple", 'Banana' => &$vr, 'Pear' => "green");

    $vr = "white";
    echo $arr['Banana'];

?>

A reference has been assigned as value to the key, 'Banana'. The output is:

    white

Copying an Array
When copying an array to a variable, references to keys are not copied - they cannot be copied and should not be copied. The syntax to copy an array in PHP is just:

    $copy_of_array = $array;

Try the following code, which shows that the reference to a key has not been copied:

<?php

    $vr = "yellow";

    $arr = array('Apple' => "purple", 'Banana' => &$vr, 'Pear' => "green");

    echo $arr['Banana'], '<br>';
    $vr = "white";
    echo $arr['Banana'], '<br>';
    $arr2 = $arr;
    $vr = "black";
    echo $arr['Banana'], '<br>';

?>

The output is:

    yellow
    white
    black

Object Reference
An object variable can be referenced. When this is done, the references inside the object remain. Try the following code:

<?php

    $vr = 'I love you.';

    class Cla
        {
            public $prop;

            function __construct()
                {
                    global $vr;
                    $this->prop = &$vr;
                }
        }

    $obj = new Cla();
    $obj1 = &$obj;
    echo $obj1->prop, '<br>';
    $vr = 'I hate you.';
    echo $obj1->prop, '<br>';
    $obj2 = clone $obj1;
    $vr = 'Why?';
    echo $obj2->prop;

?>

The output is:

    I love you.
    I hate you.
    Why?

Array Reference
An array variable can be referenced. When this is done, the references inside the array remain. Try the following code:

<?php

    $vr = "yellow";

    $arr = array('Apple' => "purple", 'Banana' => &$vr, 'Pear' => "green");

    $arr1 = $arr;
    echo $arr1['Banana'], '<br>';
    $vr = "white";
    echo $arr1['Banana'], '<br>';
    $arr2 = $arr1;
    $vr = "black";
    echo $arr['Banana'], '<br>';

?>

The output is:

    yellow
    white
    black

That is it for this part of the series.

Chrys


Related Links

Basics of PHP with Security Considerations
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
More Related Links

Cousins

BACK

Comments