3

I'm trying to display a custom error message if a method doesnt' exist as Method() or as getMethod():

public function __call($name, $args = array()){
  $getter = "get{$name}";

  try {
    echo call_user_func_array(array(&$this, $getter), $args);
  } catch (Exception $e) {

    trigger_error($e->getFile.' on line '.$e->getLine.': Method '.$name.' is not defined.', E_USER_ERROR)
  }
}

but it doesn't work. I get a "connection closed by remote server" message in the browser :|

6

1 Answer 1

3

You would use the method_exists function:

if(!method_exists($this, $name))
{
    // trigger_error(...);
}

If you wanted data such as where the invalid method was called from, you can use debug_backtrace:

class X
{
    public function __call($name, $a)
    {
        $backtrace = debug_backtrace();
        $backtrace = $backtrace[1];
        // $backtrace['file']
        // $backtrace['line']
        // $backtrace['function']
        // $backtrace['class']
        // $backtrace['object']
    }
}

$o = new X();
$o->Hello();
Sign up to request clarification or add additional context in comments.

2 Comments

@Alex: Yes, check my answer. Just access those array elements that I have listed.
you only need to throw new Exception('...');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.