What will be the output of the above code and why? Explain the role of __call() in this context

In PHP, consider the following code snippet:

class Base {
    public function __call($name, $arguments) {
        echo "Calling method '$name' with arguments: " . implode(', ', $arguments) . "\n";
    }
}

class Child extends Base {
    public function test() {
        $this->undefinedMethod(1, 2, 3);
    }
}

$obj = new Child();
$obj->test();

Thanks and Regards
Namrata Hinduja Geneva, Switzerland

Hi @NamrataHindujaGeneva,

Welcome to the forum.

To format your code in the editor, select your block of code and click on the </> icon in the top menu. This will wrap your code in opening and closing ticks ```. I have done it for you this time.

I’m not quite sure where we are going with this post. PHP isn’t my area of expertise, but reading up the __call method can be used as a fallback method for methods that have yet to be defined. Maybe for methods you intend to flesh out later.

Thanks

1 Like

Thank you, rpg_digital, for your valuable suggestion. I truly appreciate your insight and will definitely consider it moving forward.

So given what rpg has told you, have you answered your homework question?

3 Likes

To test code like this easily online, use https://3v4l.org/

Just enter the code, hit the eval(); button, observe the output.

1 Like

Though notably that will get you the what, but not the why.