The topic on how objects and variable references are controlled in memory is met with different opinions when it comes to PHP. A popular question is whether by default objects are passed by reference or by copy in PHP. To get to understand the actual concept, you’ll need to know the following:
- What a reference is in PHP.
- What a reference is not in PHP.
- How does the garbage collector function in PHP?
In our age and time, memory is not the pricey and limited resource that it was in the past. This could be why some PHP developers do not understand how variables and objects are controlled internally during execution of applications. Take a statement $a = new Foo(); . How does PHP create objects in memory when performing that statement? To figure out whether the objects are passed (by default) either by reference or by copy, let’s analyze the term “reference” in PHP.
Why is a reference not in PHP?
As much as you need to know what a reference is, you need to know what it is not first. It is important to note that a reference is not a C-style pointer in PHP. Unlike with C pointers, you can’t perform any arithmetic functions with references in PHP. This is because PHP references are not memory addresses: they are not numbers that indicate a memory location.
What then is a reference?
References in PHP are “aliases” that let two different variables read and write a single value. They can also be referred to as mechanisms that give access to the same values from values bearing different names so that they can function like they are the same variable. Remember that in PHP, variable names and their contents are different things, linked in the “symbol’s table.” When a reference is created, it adds an alias for the created variable in the symbol table.
Take this code for instance:
$a = new Foo();
When the statement is executed, variable $a is created in memory; object type Foo is also created in memory. An entry is added to the symbol table indicating that variable $a “references” (is related to/points to) object Foo , but it isn’t necessarily a pointer to that object.
If you execute the statement:
$b = $a;
$b does not become a reference or a copy of $a. We only created a new variable $b in memory and created a new entry in the symbol’s table showing that variable $b references to object Foo which $a does.
Take the statement:
$c = &$a;
We have introduced new variable $c in memory, but not a new entry for $c in the symbols table. In the symbols table, $c is an alias for $a, so it will act similarly, but unlike in C, $c is not a pointer to $a.
When we want to modify the value of any of the three valuables, PHP will create a z_val structure in memory to separate the values of variable $b from those of the pair $a/$c . Each then can be modified independently without affecting the other’s value.
How does the garbage collector function in PHP Memory?
When there are no references to a particular object in the symbols table, the object or variable in the PHP memory will be removed by PHP garbage collector. PHP keeps a reference counter of an object from its creation so that during the execution of the script PHP, the counter increments and decrements that reference counter based on the variables that are “pointing” to it. Once the reference count gets to 0 showing that nothing is referencing that object meaning it is not being used, PHP marks that object “removable” so that it is removed from the memory by the next PHP garbage collector. This frees up space for reuse.
To get a clearer and more in depth understanding of objects, references and PHP Memory, visit here. It is a great article with examples.
To read other interesting discussions about software topics, visit our dedicated software page.