In PHP, objects are they passed by value or by reference?
In PHP, objects are they passed by value or by reference?
In PHP, objects are passed by reference by default. This means that when you pass an object as an argument to a function or assign it to a new variable, both the original variable and the new one refer to the same object in memory.
Here's an example to illustrate this:
class Person {
public $name;
public function __construct($name) {
$this->name = $name;
}
}
$person1 = new Person("John");
$person2 = $person1;
echo $person1->name; // Output: John
echo $person2->name; // Output: John
$person2->name = "Jane";
echo $person1->name; // Output: Jane
echo $person2->name; // Output: Jane
In this example, when we assign $person1
to $person2
...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào