Câu hỏi phỏng vấn PHP
Câu hỏi

In PHP, objects are they passed by value or by reference?

Câu trả lời

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:

php Copy
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

junior

Gợi ý câu hỏi phỏng vấn

junior

What are the main differences between const vs define

junior

Is there a difference between isset and !empty ?

senior

How to turn errors into exceptions in PHP?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào