Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查同一类中是否存在多个方法?_Php_Class - Fatal编程技术网

Php 检查同一类中是否存在多个方法?

Php 检查同一类中是否存在多个方法?,php,class,Php,Class,是否有方法验证同一类中是否存在多个方法 class A{ function method_a(){} function method_b(){} } if ( (int)method_exists(new A(), 'a', 'b') ){ echo "Method a & b exist"; } 您需要单独检查每个方法: $a = new A(); if(method_exists($a, 'method_a'))... if(method_exists(

是否有方法验证同一类中是否存在多个方法

class A{
    function method_a(){}
    function method_b(){}
}

if ( (int)method_exists(new A(), 'a', 'b') ){
    echo "Method a & b exist";
}

您需要单独检查每个方法:

$a = new A();

if(method_exists($a, 'method_a'))...
if(method_exists($a, 'method_b'))...

在一个函数调用中不能检查多个方法

您需要单独检查每个方法:

$a = new A();

if(method_exists($a, 'method_a'))...
if(method_exists($a, 'method_b'))...

您不能在一个函数调用中检查多个方法

不要认为这种函数性存在,但您可以尝试比较类方法数组和您的方法,例如:

$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';

不要认为存在这样的函数性,但您可以尝试比较类方法数组和您的方法,例如:

$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';

我可能会在这里使用界面:

interface Foo {
  function a();
  function b();
}
。。。然后,在客户端代码中:

if (A instanceof Foo) {
   // it just has to have both a() and b() implemented
}

我认为这比检查方法的存在更清楚地显示了您的真实意图。

我可能会在这里使用界面:

interface Foo {
  function a();
  function b();
}
。。。然后,在客户端代码中:

if (A instanceof Foo) {
   // it just has to have both a() and b() implemented
}
我认为这比检查方法的存在更清楚地显示了您的真实意图。

使用:

你可以在这里拉小提琴:

使用:


你可以在这里拉小提琴:

没错。这是有意义的,因为方法名称是单数的,而不是复数的。这是有意义的,因为方法名称是单数的,而不是复数的。