Complete the code to define a magic method that is called when an undefined property is accessed.
<?php class Test { public function [1]($name) { return "Property $name does not exist."; } } $test = new Test(); echo $test->foo; ?>
The __get magic method is called when accessing an undefined or inaccessible property.
Complete the code to define a magic method that is called when an undefined method is called.
<?php class Test { public function [1]($name, $arguments) { return "Method $name does not exist."; } } $test = new Test(); echo $test->foo(); ?>
The __call magic method is triggered when calling inaccessible or undefined methods.
Fix the error in the magic method that sets a property value.
<?php class Test { public function [1]($name, $value) { $this->$name = $value; } } $test = new Test(); $test->foo = 'bar'; echo $test->foo; ?>
The __set magic method is called when setting a value to an undefined or inaccessible property.
Fill both blanks to create a magic method that returns a string when the object is treated as a string.
<?php class Test { public function [1]() { return [2]; } } $test = new Test(); echo $test; ?>
The __toString magic method defines what happens when an object is converted to a string. It must return a string.
Fill all three blanks to create a magic method that is called when the object is used as a function.
<?php class Test { public function [1]([2]) { return "Called with argument: " . [3]; } } $test = new Test(); echo $test('hello'); ?>
The __invoke magic method is called when an object is used like a function. It can accept arguments.