How can an object in PHP be extended with a new variable?
To extend an object in PHP with a new variable, you can simply add a new property to the object using the arrow operator (->). This allows you to dynamically add new variables to an object without modifying the class definition. By doing this, you can easily extend the functionality of an object at runtime.
class MyClass {
public $existingVariable = 'Hello';
}
$object = new MyClass();
$object->newVariable = 'World';
echo $object->existingVariable . ' ' . $object->newVariable; // Output: Hello World