PHP Tutorials - Herong's Tutorial Examples - v5.17, by Herong Yang
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 2015 (c) 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:
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.
Table of Contents
Introduction and Installation of PHP
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
Interface with Operating System
►Introduction of Class and Object
What Is a Superclass and a Subclass
What Is an Overloaded Property
What Is Object Property Iteration
►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
Parsing and Managing HTML Documents
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS