search for in the  
<is_subclass_ofproperty_exists>
Last updated: Thu, 19 May 2005

method_exists

(PHP 4, PHP 5)

method_exists -- Checks if the class method exists

Description

bool method_exists ( object object, string method_name )

This function returns TRUE if the method given by method_name has been defined for the given object, FALSE otherwise.

Example 1. method_exists() example

<?php
$directory
= new Directory('.');
var_dump(method_exists($directory,'read'));
?>

The above example will output:

bool(true)



User Contributed Notes
method_exists
jpgiot at nospam ifrance.com
06-May-2004 09:42
a little difference :

to find a method of an object (instance of a class)

<?php
if (method_exists($myinstance,'themethod'))
   echo
'ok';
?>

to find a method of a class (using the class name, not the instance of the class!)

<?php
if (is_callable(array('theclassname','themethod')))
   echo
'ok';
?>
Thomas@ThBeckmann
31-Jan-2003 03:47
Though, as Bejamin noted, it's not possible to use the class name in method_exists within the class definition, get_class_methods delivers the method names for a given class name even inside the class. Thus another workaround for the mentioned problem is to use in_array(<method_name>, get_class_methods(<class_name>))
benjamin_ansbach at web dot de
27-Dec-2002 02:49
if you want to check for a method "inside" of a class use:

method_exists($this, 'function_name')

i was a bit confused 'cause i thought i'm only able to check for a method when i got an object like $object_name = new class_name() with:

method_exists($object_name, 'method_name')

small example for those who didn't understood what i mean ( maybe caused by bad english :) ):

<?php

class a {

   function
a() {
      
       if(
method_exists($this, 'test'))
           echo
'a::test() exists!';
       else
           echo
'a::test() doesn\'t exists';

   }

  
   function
test() {
      
       return
true;
  
   }

}

$b = new a();

?>

the output will be: a::test() exists!

maybe this will help someone
j dot metzger at steptown dot com
16-Jan-2002 05:42
call_user_method uses the same mechanism as a normal method call. So you can get the returned values as well in this way.

$pagetext=call_user_method($method,$object_call);

All information is then in $pagetext.

<is_subclass_ofproperty_exists>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT