How can one address the issue of passing variables by reference in PHP 7.0 when creating a new instance of an object within a function call?

When creating a new instance of an object within a function call in PHP 7.0, the issue of passing variables by reference arises because objects are always passed by reference in PHP. To solve this, you can use the clone keyword to create a copy of the object and pass it by value instead of by reference.

class MyClass {
    public $value;
    
    public function __construct($value) {
        $this->value = $value;
    }
}

function createObject($obj) {
    $newObj = clone $obj;
    $newObj->value = "Modified value";
    return $newObj;
}

$obj = new MyClass("Original value");
$newObj = createObject($obj);

echo $obj->value; // Outputs: Original value
echo $newObj->value; // Outputs: Modified value