note 103635 modified in function.method-exists by danbrown

From: Date: Mon, 25 Apr 2011 17:07:03 +0000
Subject: note 103635 modified in function.method-exists by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-179122@lists.php.net to get a copy of this message
Using method_exists inside an object's __call() method can be very usefull if you want to avoid to get a fatal error because of a limit in function nesting or if you are calling methods that dont exist but need to continue in your application: <?php class Something { /** * Call a method dynamically * * @param string $method * @param array $args * @return mixed */ public function __call($method, $args) { if(method_exists($this, $method)) { return call_user_func_array(array($this, $method), $args); }else{ throw new Exception(sprintf('The required method "%s" does not exist for %s', $method, get_class($this))); } } } ?> --was-- Using method_exists inside an object's __call() method can be very usefull if you want to avoid to get a fatal error because of a limit in function nesting or if you are calling methods that dont exist but need to continue in your application: <?php class Something { /** * Call a method dynamically * * @param string $method * @param array $args * @return mixed */ public function __call($method, $args) { if(method_exists($this, $method)) { return call_user_func_array(array($this, $method), $args); }else{ throw new Exception(sprintf('The required method "%s" does not exist for %s', $method, get_class($this))); } } } http://php.net/manual/en/function.method-exists.php

« previous php.notes (#179122) next »