Sobes.tech
Junior — Middle

What happens to an object after it is passed to a function and its state is subsequently changed inside the function?

sobes.tech AI

Answer from AI

In PHP, objects are passed to functions by reference, meaning that inside the function you work with the same object as outside. If you change the state of the object inside the function (for example, modify one of its properties), these changes will be visible after the function exits.

Example:

class User {
    public $name;
}

function changeName($user) {
    $user->name = "New Name";
}

$user = new User();
$user->name = "Old Name";
changeName($user);
echo $user->name; // Will output: New Name

Thus, changing the state of the object inside the function affects the object itself, not its copy.

What happens to an object after it is passed to a… - sobes.tech