0
0
PHPprogramming~20 mins

__call and __callStatic in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP Magic Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP code using __call?

Consider the following PHP class with a __call magic method. What will be printed when the code runs?

PHP
<?php
class Test {
    public function __call($name, $arguments) {
        echo "Called method '$name' with arguments: " . implode(", ", $arguments) . "\n";
    }
}

$obj = new Test();
$obj->hello('world', 123);
?>
ACalled method 'hello' with arguments: world, 123
BFatal error: Call to undefined method Test::hello()
CCalled method 'hello' with arguments: world
DCalled method 'hello' with arguments: 123, world
Attempts:
2 left
💡 Hint

The __call method catches calls to undefined instance methods and receives the method name and arguments.

Predict Output
intermediate
2:00remaining
What happens when calling an undefined static method with __callStatic?

Given this PHP class with a __callStatic method, what will be the output?

PHP
<?php
class StaticTest {
    public static function __callStatic($name, $arguments) {
        echo "Static call to '$name' with args: " . implode(", ", $arguments) . "\n";
    }
}

StaticTest::foo('a', 'b');
?>
AStatic call to 'foo' with args: b, a
BFatal error: Call to undefined static method StaticTest::foo()
CStatic call to 'foo' with args: a, b
DCalled method 'foo' with arguments: a, b
Attempts:
2 left
💡 Hint

The __callStatic method handles calls to undefined static methods.

🔧 Debug
advanced
2:00remaining
Why does this code cause a fatal error instead of calling __callStatic?

Examine this PHP code. Why does it cause a fatal error instead of invoking __callStatic?

PHP
<?php
class Demo {
    public function __callStatic($name, $args) {
        echo "Called static method $name";
    }
}

Demo::test();
?>
AThe method name 'test' is reserved and cannot be used
B__callStatic is only called for instance methods, not static methods
CThe class Demo must extend a base class to use __callStatic
D__callStatic must be declared static, but here it is declared as an instance method
Attempts:
2 left
💡 Hint

Check the method declaration for __callStatic and whether it is static.

📝 Syntax
advanced
2:00remaining
Which option correctly defines both __call and __callStatic methods?

Choose the option that correctly declares both __call and __callStatic magic methods in a PHP class.

Apublic static function __call($name, $args) { } public function __callStatic($name, $args) { }
Bpublic function __call($name, $args) { } public static function __callStatic($name, $args) { }
Cpublic function __call($name) { } public static function __callStatic($name) { }
Dpublic function __call($name, $args) { } public function __callStatic($name, $args) { }
Attempts:
2 left
💡 Hint

Remember that __call is for instance methods and __callStatic is for static methods.

🚀 Application
expert
3:00remaining
How many items are in the array after these calls using __call and __callStatic?

Given this PHP class that stores called method names in arrays via __call and __callStatic, how many items are in $obj->calls and Test::$staticCalls after the code runs?

PHP
<?php
class Test {
    public $calls = [];
    public static $staticCalls = [];

    public function __call($name, $args) {
        $this->calls[] = $name;
    }

    public static function __callStatic($name, $args) {
        self::$staticCalls[] = $name;
    }
}

$obj = new Test();
$obj->foo();
$obj->bar();
Test::baz();
Test::qux();
?>
A$obj->calls has 2 items; Test::$staticCalls has 2 items
B$obj->calls has 4 items; Test::$staticCalls has 0 items
C$obj->calls has 0 items; Test::$staticCalls has 4 items
D$obj->calls has 1 item; Test::$staticCalls has 3 items
Attempts:
2 left
💡 Hint

Count how many times instance and static undefined methods are called.