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 );
is there any way that I can ignore functions.... Definitely not what I want. Will take note of answers though.