Guntis
Guntis

Reputation: 510

php class error handling

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?

Upvotes: 0

Views: 75

Answers (4)

JvdBerg
JvdBerg

Reputation: 21856

class Foo {
    private $_bar;

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

    public function __call($name, $params)
    {
       throw new Exception("Method $name does not exists!");
    }
}

Upvotes: 3

xkeshav
xkeshav

Reputation: 54016

write __call in Class Foo

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

Reference

Upvotes: 0

dead
dead

Reputation: 364

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

And throw your own exception from there.

Upvotes: 3

Madara's Ghost
Madara's Ghost

Reputation: 174957

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);.

Upvotes: 0

Related Questions