Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: method_exists - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

property_exists" width="11" height="7"/> <is_subclass_of
Last updated: Thu, 31 May 2007

view this page in

method_exists

(PHP 4, PHP 5)

method_exists — クラスメソッドが存在するかどうかを確認する

説明

bool method_exists ( object $object, string $method_name )

指定した object にクラスメソッドが存在するかどうかを調べます。

パラメータ

object

オブジェクトのインスタンス。

method_name

メソッドの名前。

返り値

method_name で指定したメソッドが 指定した object において定義されている場合に TRUE、そうでない場合に FALSE を返します。

例 378. method_exists() の例

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

上の例の出力は以下となります。


bool(true)

    

参考

function_exists()
is_callable()



property_exists" width="11" height="7"/> <is_subclass_of
Last updated: Thu, 31 May 2007
 
add a note add a note User Contributed Notes
method_exists
04-Dec-2006 07:14
Not (yet) mentioned in the docs: since PHP 5 the first argument may also be a string with the class name:

class foo {
function bar () {}
}

var_dump(method_exists('foo','bar'));

Will output bool(true) - however note that this won't work in PHP 4. When running PHP 4 this will output bool(false).

Tested with PHP 5.1 / PHP 4.3
jp at function dot fi
30-Apr-2006 06:29
As mentioned before, is_callable and method_exists report all methods callable even if they are private/protected and thus actually not callable. So instead of those functions you may use following work-around which reports methods as supposed to.

<?php
class Foo1 {
 
public function bar() {
    echo
"I'm private Foo1::bar()";
  }
}

class
Foo2 {
 
private function bar() {
    echo
"I'm public Foo2::bar()";
  }
}

$f1=new Foo1;
$f2=new Foo2;

if(
is_callable(array($f1,"bar"))) {
    echo
"Foo1::bar() is callable";
} else {
    echo
"Foo1::bar() isn't callable";
}
if(
is_callable(array($f2,"bar"))) {
    echo
"Foo2::bar() is callable";
} else {
    echo
"Foo2::bar() isn't callable";
}
if(
in_array("bar",get_class_methods($f1))) {
    echo
"Foo1::bar() is callable";
} else {
    echo
"Foo1::bar() isn't callable";
}
if(
in_array("bar",get_class_methods($f2))) {
    echo
"Foo2::bar() is callable";
} else {
    echo
"Foo2::bar() isn't callable";
}

?>

output
Foo1::bar() is callable (correct)
Foo2::bar() is callable (incorrect)
Foo1::bar() is callable (correct)
Foo2::bar() isn't callable (correct)

?>
seufert at gmail dot com
27-Apr-2006 10:27
Just a note that the behaviour of this function changed between version 5.0.x and 5.1.x when using static member functions

Using this code:
<?php
class a {
    static function
test() {return "A";}
}
if(
method_exists('a','test'))
    print
call_user_func(array('a','test'));
else
    print
"Nothing";
?>
PHP 5.1.x returns "A"
PHP 5.0.x returns "Nothing"

Im not sure of a workaround for PHP 5.0.x yet.
spam at majiclab dot com
01-Feb-2006 01:33
Both method_exists() and is_callable() return private and protected functions, which, as mentioned below, causes problems for PHP5/OO programming.  You can use get_class_methods() with either an $instance of a class or the 'ClassName' to get only public functions.
daniel at softel dot jp
10-Jan-2006 12:34
Note that in PHP5, method_exists() will sucessfully find *private* methods. This has some OO/data-hiding ramifications.
jpgiot at nospam ifrance.com
06-May-2004 11: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
01-Feb-2003 06: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
28-Dec-2002 05: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 08: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.

property_exists" width="11" height="7"/> <is_subclass_of
Last updated: Thu, 31 May 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites