What are some common pitfalls to avoid when passing objects by reference in PHP functions?

One common pitfall to avoid when passing objects by reference in PHP functions is unintentionally modifying the original object outside of the function. To prevent this, ensure that the object is explicitly passed by reference using the `&` symbol in the function parameter declaration. This way, any changes made to the object within the function will reflect on the original object.

// Incorrect way of passing object by reference
function modifyObject($obj) {
    $obj->property = 'modified';
}

$obj = new stdClass();
$obj->property = 'original';

modifyObject($obj);

echo $obj->property; // Outputs 'original'

// Correct way of passing object by reference
function modifyObject(&$obj) {
    $obj->property = 'modified';
}

$obj = new stdClass();
$obj->property = 'original';

modifyObject($obj);

echo $obj->property; // Outputs 'modified'