note 65405 added to function.method-exists

From: Date: Sun, 30 Apr 2006 09:29:48 +0000
Subject: note 65405 added to function.method-exists
Groups: php.notes 
Request: Send a blank email to php-notes+get-110164@lists.php.net to get a copy of this message
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) ?> ---- Server IP: 80.81.171.181 Probable Submitter: 84.253.195.244 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.method-exists.php Edit -- http://master.php.net/note/edit/65405 Del: integrated -- http://master.php.net/note/delete/65405/integrated Del: useless -- http://master.php.net/note/delete/65405/useless Del: bad code -- http://master.php.net/note/delete/65405/bad+code Del: spam -- http://master.php.net/note/delete/65405/spam Del: non-english -- http://master.php.net/note/delete/65405/non-english Del: in docs -- http://master.php.net/note/delete/65405/in+docs Del: other reasons-- http://master.php.net/note/delete/65405 Reject -- http://master.php.net/note/reject/65405 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#110164) next »