0
0
PHPprogramming~10 mins

__call and __callStatic in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the magic method that handles calls to undefined instance methods.

PHP
<?php
class Test {
    public function [1]($name, $arguments) {
        echo "Called method: $name";
    }
}
$test = new Test();
$test->hello();
Drag options to blanks, or click blank then click option'
A__get
B__callStatic
C__set
D__call
Attempts:
3 left
💡 Hint
Common Mistakes
Using __callStatic instead of __call for instance methods.
Confusing __call with property access magic methods like __get or __set.
2fill in blank
medium

Complete the code to define the magic method that handles calls to undefined static methods.

PHP
<?php
class Test {
    public static function [1]($name, $arguments) {
        echo "Called static method: $name";
    }
}
Test::hello();
Drag options to blanks, or click blank then click option'
A__call
B__destruct
C__callStatic
D__invoke
Attempts:
3 left
💡 Hint
Common Mistakes
Using __call instead of __callStatic for static methods.
Confusing __callStatic with other magic methods like __invoke.
3fill in blank
hard

Fix the error in the code by completing the magic method name that handles undefined instance method calls.

PHP
<?php
class Demo {
    public function [1]($method, $args) {
        echo "Method $method called with arguments: " . implode(', ', $args);
    }
}
$obj = new Demo();
$obj->test(1, 2);
Drag options to blanks, or click blank then click option'
A__call
B__callStatic
C__get
D__set
Attempts:
3 left
💡 Hint
Common Mistakes
Using __callStatic instead of __call for instance methods.
Forgetting the method must be public.
4fill in blank
hard

Fill both blanks to complete the class that handles undefined instance and static method calls.

PHP
<?php
class Magic {
    public function [1]($name, $args) {
        echo "Instance method $name called.";
    }
    public static function [2]($name, $args) {
        echo "Static method $name called.";
    }
}
Magic::foo();
$magic = new Magic();
$magic->bar();
Drag options to blanks, or click blank then click option'
A__call
B__invoke
C__callStatic
D__get
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up __call and __callStatic.
Not declaring __callStatic as static.
5fill in blank
hard

Fill all three blanks to create a class that logs calls to undefined instance and static methods with method names and arguments.

PHP
<?php
class Logger {
    public function [1]($name, $args) {
        echo "Instance call: $name with args: " . implode(', ', $[2]) . "\n";
    }
    public static function [3]($name, $args) {
        echo "Static call: $name with args: " . implode(', ', $args) . "\n";
    }
}
$log = new Logger();
$log->run(5, 10);
Logger::start('a', 'b');
Drag options to blanks, or click blank then click option'
A__call
Bargs
C__callStatic
Darguments
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable name for arguments.
Confusing __call and __callStatic.
Not declaring __callStatic as static.