Bird
Raised Fist0
PHPprogramming~20 mins

__call and __callStatic in PHP - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the __call magic method in PHP?
easy
A. To automatically serialize objects
B. To handle calls to undefined static methods
C. To handle calls to undefined instance methods
D. To overload operators like + and -

Solution

  1. Step 1: Understand the role of __call

    The __call method is triggered when an undefined instance method is called on an object.
  2. Step 2: Differentiate from __callStatic

    __callStatic handles undefined static method calls, not instance calls.
  3. Final Answer:

    To handle calls to undefined instance methods -> Option C
  4. Quick Check:

    __call = instance method handler [OK]
Hint: Instance missing methods use __call, static ones use __callStatic [OK]
Common Mistakes:
  • Confusing __call with __callStatic
  • Thinking __call handles static methods
  • Assuming __call serializes objects
2. Which of the following is the correct syntax to declare the __callStatic method in a PHP class?
easy
A. public static function __callStatic($name, $arguments)
B. public function __callStatic($name, $arguments)
C. static function __callStatic($name, $arguments)
D. function __callStatic($name, $arguments)

Solution

  1. Step 1: Recall method declaration rules for static magic methods

    The __callStatic method must be declared as public static function to handle static calls.
  2. Step 2: Check each option's syntax

    public static function __callStatic($name, $arguments) correctly uses public static function. Options B and D miss static, and C misses public.
  3. Final Answer:

    public static function __callStatic($name, $arguments) -> Option A
  4. Quick Check:

    Static magic methods need 'public static' [OK]
Hint: Static magic methods need 'public static' keyword [OK]
Common Mistakes:
  • Omitting 'static' keyword
  • Omitting 'public' visibility
  • Using wrong parameter names
3. What will be the output of the following PHP code?
class Test {
    public function __call($name, $args) {
        echo "Called method: $name with argument: " . $args[0];
    }
}

$obj = new Test();
$obj->hello('world');
medium
A. Called method: hello with argument: world
B. Fatal error: Call to undefined method Test::hello()
C. Called method: __call with argument: world
D. No output

Solution

  1. Step 1: Identify method call and class behavior

    The method hello is not defined in Test, so __call is triggered.
  2. Step 2: Understand __call output

    __call echoes the method name and first argument, so it prints "Called method: hello with argument: world".
  3. Final Answer:

    Called method: hello with argument: world -> Option A
  4. Quick Check:

    Undefined instance method triggers __call [OK]
Hint: Undefined instance calls trigger __call with method name [OK]
Common Mistakes:
  • Expecting fatal error instead of __call handling
  • Confusing __call with __callStatic
  • Ignoring argument array usage
4. Consider this PHP code snippet:
class Demo {
    public static function __callStatic($name, $args) {
        echo "Static call to $name";
    }
}

Demo::test();

What is the error in this code?
medium
A. Missing public keyword in __callStatic declaration
B. Cannot call static method __callStatic directly
C. Class Demo must define __call method for static calls
D. No error; code runs and prints 'Static call to test'

Solution

  1. Step 1: Check method declaration and call

    __callStatic is declared correctly as public static function and is called via Demo::test().
  2. Step 2: Understand magic method behavior

    Since test is undefined static method, __callStatic is triggered and echoes the message.
  3. Final Answer:

    No error; code runs and prints 'Static call to test' -> Option D
  4. Quick Check:

    Undefined static calls trigger __callStatic [OK]
Hint: Undefined static calls invoke __callStatic automatically [OK]
Common Mistakes:
  • Thinking __call is needed for static calls
  • Assuming direct call to __callStatic is required
  • Missing 'public' or 'static' keywords
5. You want to create a PHP class that logs every call to undefined instance or static methods with their names and arguments. Which approach correctly uses __call and __callStatic to achieve this?
hard
A. Use normal methods with logging inside; magic methods cannot log undefined calls
B. Implement __call for instance methods and __callStatic for static methods, both logging method name and args
C. Implement only __callStatic to catch all method calls, static and instance
D. Implement only __call to catch all method calls, static and instance

Solution

  1. Step 1: Understand magic methods roles

    __call handles undefined instance method calls; __callStatic handles undefined static method calls.
  2. Step 2: Combine both for full coverage

    To log all undefined calls, implement both methods to log method names and arguments accordingly.
  3. Final Answer:

    Implement __call for instance methods and __callStatic for static methods, both logging method name and args -> Option B
  4. Quick Check:

    Use both __call and __callStatic for full logging [OK]
Hint: Use both __call and __callStatic to catch all undefined calls [OK]
Common Mistakes:
  • Using only one magic method for both call types
  • Thinking magic methods cannot log calls
  • Confusing instance and static method handling
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard