Updating Variables Who Share an Object

This section describes what is in an object variable - An object variable actually contains the object identifier that points to where the object is stored.



From previous section, we learned that an object can be shared by two variables in two ways. Now let's see what will the impact on the other variable, if we updating one variable who shares an object with the other object. Here is my understanding:

The tutorial example below shows those types object sharing and how updating one variable impacts the other variable:

<?php 
/* SharedObjectTest.php
#- Copyright (c) 2015-2019, HerongYang.com, All Rights Reserved.
*/

class C {
   public $size;
   public function __construct($s = "Medidum") {
      $this->size = $s;
   }
}
   echo "Test on No Sharing:\n";
   $i = new C();
   $j = new C();
   echo "   (i,j)=(".$i->size.",".$j->size.") before updating \$j\n";
   $j->size = "Large";
   echo "   (i,j)=(".$i->size.",".$j->size
      .") after updating the object of \$j\n";
   $j = new C("Small");
   echo "   (i,j)=(".$i->size.",".$j->size
      .") after updating the content of \$j\n";

   echo "Test on Soft Sharing:\n";
   $a = new C();
   $b = $a;
   echo "   (a,b)=(".$a->size.",".$b->size.") before updating \$b\n";
   $b->size = "Large";
   echo "   (a,b)=(".$a->size.",".$b->size
      .") after updating the object of \$b\n";
   $b = new C("Small");
   echo "   (a,b)=(".$a->size.",".$b->size
      .") after updating the content of \$b\n";

   echo "Test on Hard Sharing:\n";
   $x = new C();
   $y = &$x;
   echo "   (x,y)=(".$x->size.",".$y->size.") before updating \$y\n";
   $y->size = "Large";
   echo "   (x,y)=(".$x->size.",".$y->size
      .") after updating the object of \$y\n";
   $y = new C("Small");
   echo "   (x,y)=(".$x->size.",".$y->size
      .") after updating the content of \$y\n";
?>

If you run the above tutorial example, you will get something like this:

C:\herong> \php\php SharedObjectTest.php

Test on No Sharing:
   (i,j)=(Medidum,Medidum) before updating $j
   (i,j)=(Medidum,Large) after updating the object of $j
   (i,j)=(Medidum,Small) after updating the content of $j
Test on Soft Sharing:
   (a,b)=(Medidum,Medidum) before updating $b
   (a,b)=(Large,Large) after updating the object of $b
   (a,b)=(Large,Small) after updating the content of $b
Test on Hard Sharing:
   (x,y)=(Medidum,Medidum) before updating $y
   (x,y)=(Large,Large) after updating the object of $y
   (x,y)=(Small,Small) after updating the content of $y

The output confirms the my understanding presented earlier.

Last update: 2019.



 

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.3

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

Introduction of Class and Object

 What Is a Class

 What Is an Object

 What Is a Constructor

 What Is a Static Method

 What Is a Static Variable

 What Is a Superclass and a Subclass

 What Is an Abstract Class

 What Is an Abstract Method

 What Is an Interface

 What Is a Trait

 What Is an Overloaded Property

 What Is an Overloaded Method

 What Is Object Property Iteration

 What Is Object Cloning

 What Is Object Serialization

 What Is in an Object Variable

Updating Variables Who Share an Object

 Passing Objects as Function Arguments

 Integrating PHP with Apache Web Server

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Configuring and Sending out Emails

 Outdated Tutorials

 References

 Full Version in PDF/EPUB