Providing a callee response to a “call to undefined method” error? [duplicate]

I’m learning my way into some more advanced programming with PHP.

I’ve seen that calling an non-existent method produces “call to undefined method” error.

PHP being quite flexible is there a technique that allows to intercept this error ? If so, how is this usually done ?

Edit: To clarify, I want to actually do something when the error occurs like send a reply back, not necessarily prevent it. Forget to mention that this is in the context of a class. Of course, methods only apply in the context of a class 😉

Comments 3

  • Yes, it’s possible to trap calls to undefined methods of classes using magic methods:

    You need to implement the __call() and/or __callStatic() methods as defined here.

    Suppose you have a simple class CCalculationHelper with just a few methods:

    class CCalculationHelper {
    
      static public function add( $op1, $op2 ) {
    
        assert(  is_numeric( $op1 ));
        assert(  is_numeric( $op2 ));
    
        return ( $op1 + $op2 );
    
      }
    
      static public function diff( $op1, $op2 ) {
    
        assert(  is_numeric( $op1 ));
        assert(  is_numeric( $op2 ));
    
        return ( $op1 - $op2 );
    
      }
    
    }
    

    At a later point of time, you need to enhance this class by multiplication or division. Instead of using two explicit methods, you might use a magic method, which implements both operations:

    class CCalculationHelper {
    
      /**  As of PHP 5.3.0  */
      static public function __callStatic( $calledStaticMethodName, $arguments ) {
    
        assert( 2 == count( $arguments  ));
        assert( is_numeric( $arguments[ 0 ] ));
        assert( is_numeric( $arguments[ 1 ] ));
    
        switch( $calledStaticMethodName ) {
    
           case 'mult':
              return $arguments[ 0 ] * $arguments[ 1 ];
              break;
    
           case 'div':
              return $arguments[ 0 ] / $arguments[ 1 ];
              break;
    
        }
    
        $msg = 'Sorry, static method "' . $calledStaticMethodName . '" not defined in class "' . __CLASS__ . '"';
        throw new Exception( $msg, -1 );
    
      }
    
      ... rest as before... 
    
    }
    

    Call it like so:

      $result = CCalculationHelper::mult( 12, 15 );
    
  • Seeing how you don’t wish to recover from these fatal errors, you can use a shutdown handler:

    function on_shutdown()
    {
        if (($last_error = error_get_last()) {
            // uh oh, an error occurred, do last minute stuff
        }
    }
    
    register_shutdown_function('on_shutdown');
    

    The function is called at the end of your script, regardless of whether an error occurred; a call to error_get_last() is made to determine that.

  • If you mean how to intercept non-existent method in a your custom class, you can do something like this

    <?php
        class CustomObject {
            public function __call($name, $arguments) {
                echo "You are calling this function: " . 
                $name . "(" . implode(', ', $arguments) . ")";
            }
        }
    
        $obj = new CustomObject();
        $obj->HelloWorld("I love you");
    ?>
    

    Or if you want to intercept all the error

    function error_handler($errno, $errstr, $errfile, $errline) {
        // handle error here.
        return true;
    }
    set_error_handler("error_handler");
    

发表评论

电子邮件地址不会被公开。 必填项已用*标注