0
class Foo {
    private $_bar;

    public function getBar() {
        return $this->_bar;
    }
}
$b = new Foo(); 
$b->xxx(); //xxx is an udefined method.

How to enhance the following class to throw a notice if undefined methods is called?

3
  • you can do it like the answer says, but why would you need to do something like that? Commented Sep 14, 2012 at 19:05
  • just education :) Thus, i don't know if its good practice ... Commented Sep 14, 2012 at 19:20
  • in that case, i think that in the 'standard' object paradigm it isnt. you just need to design your classes in a way that an object will never be asked for a method that doesnt have. but if you want to learn look for books about object oriented proggraming Commented Sep 14, 2012 at 19:26

4 Answers 4

3
class Foo {
    private $_bar;

    public function getBar() {
        return $this->_bar;
    }

    public function __call($name, $params)
    {
       throw new Exception("Method $name does not exists!");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

You have to define a the __call( $methodName , $args ) method.

And throw your own exception from there.

Comments

0

A PHP warning would be thrown like any other time when you call an undefined function. You need to enable it with error_reporting(E_ALL);.

1 Comment

calls to nonexistent methods are actually fatal errors, no matter what the error_reporting level is.
0

write __call in Class Foo

function __call( $functionName, $argumentsArray ) {
      echo "Function $functionName does not exist";
    }

Reference

Comments

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.