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 read other interesting discussions about software topics, visit our dedicated software page.