PHP Call Magic Method

Posted on

What is does?

If defined this method is will be ran if a method that does not exist is called.

Why would you use it?

You would use it to gracefully catch calling an undefined function call, Laravel uses this very well with its getting and setting on attributes and relationships. To see more about this please read here.

Example

class User {

    public function __call($method, $args) {

        echo "unknown method " . $method;

        return false;

    }

}

$user = new User();

$user->aFunctionThatDoesNotExist(); //returns an echo keeping your code from erroring