Difficulty: Medium
Correct Answer: In PHP 5 and later, object variables hold references to objects, so assigning or passing an object variable passes a handle that refers to the same underlying object instance, effectively reference like behaviour.
Explanation:
Introduction / Context:
This question examines how PHP handles objects when assigning them to new variables or passing them to functions. Understanding whether objects are copied or referenced is crucial for reasoning about side effects and memory usage in object oriented PHP code.
Given Data / Assumptions:
Concept / Approach:
In PHP 5 and later, an object variable does not hold the object value directly. Instead, it holds a reference like handle to an object that lives in memory. When you assign $b = $a; where $a is an object, both $a and $b point to the same object instance. Similarly, when you pass an object into a function by default, the function parameter receives a handle to the same object. Modifying properties of that object inside the function changes the original object, even though you are not using an explicit ampersand in the function signature.
Step-by-Step Solution:
Step 1: Recall the change introduced in PHP 5, where objects are handled by reference like semantics rather than as simple value copies.Step 2: Consider $a = new StdClass(); $a->x = 1; $b = $a; then $b->x = 2;.Step 3: If you echo $a->x after this code, you will see 2, which shows that both $a and $b refer to the same underlying object.Step 4: The same behaviour appears when objects are passed as function parameters; the function can modify the original object without explicit reference syntax.Step 5: Option A correctly describes this handle based model and is therefore the correct answer.
Verification / Alternative check:
A simple test script confirms the behaviour. Create a class with a public property, instantiate it, assign the object variable to another variable, and modify the property through the second variable. Observing that the first variable sees the same change proves that both hold references to the same object. This is different from scalar types like integers, which are copied by value.
Why Other Options Are Wrong:
Option B claims that objects are deeply copied every time, which is not how PHP 5 and later behave by default. Deep copying would require using clone explicitly. Option C incorrectly states that objects cannot be passed to functions. Option D suggests that objects are converted to strings, which is false and would only occur if you explicitly cast or echo an object with a __toString() implementation.
Common Pitfalls:
One common pitfall is assuming that assigning an object to another variable isolates changes, which leads to unexpected shared state. Another is failing to use clone when a real copy is required. Understanding that object variables are references to a shared instance helps you design methods and functions with clearer expectations about side effects.
Final Answer:
In PHP 5 and later, object variables hold references to objects, so assigning or passing an object variable passes a handle that refers to the same underlying object instance, effectively reference like behaviour.
Discussion & Comments