Is there a way to verify if multiple methods exists in same class?
class A{
function method_a(){}
function method_b(){}
}
if ( (int)method_exists(new A(), 'a', 'b') ){
echo "Method a & b exist";
}
I'd probably have used interface here:
interface Foo {
function a();
function b();
}
... then, in the client code:
if (A instanceof Foo) {
// it just has to have both a() and b() implemented
}
I think this more clearly shows your real intent then just checking for methods' existence.
You need to check each method individually:
$a = new A();
if(method_exists($a, 'method_a'))...
if(method_exists($a, 'method_b'))...
You can't check more than one method in one function call
Don't think that such functionlity exists, but you can try get_class_methods and compare arrays of class methods and your methods, e.g:
$tested_methods = array('a', 'b', 'c');
if (sizeof($tested_methods) == sizeof(array_intersect($tested_methods, get_class_methods("class_name"))))
echo 'Methods', implode(', ', $tested_methods), ' exist in class';
use get_class_methods:
class A {
function foo() {
}
function bar() {
}
}
if (in_array("foo", get_class_methods("A")))
echo "foo in A, ";
if (in_array("bar", get_class_methods("A")))
echo "bar in A, ";
if (in_array("baz", get_class_methods("A")))
echo "baz in A, ";
// output: "foo in a, bar in a, "
You can fiddle here: http://codepad.org/ofEx4FER