php 找出静态类中是否存在方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1156593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:16:31  来源:igfitidea点击:

Find out if a method exists in a static class

phpstaticobject

提问by Tyler Carter

I want to check is a function exists in a library that I am creating, which is static. I've seen function and method_exists, but haven't found a way that allows me to call them in a relative context. Here is a better example:

我想检查我正在创建的库中是否存在一个函数,它是静态的。我见过 function 和 method_exists,但还没有找到一种方法可以让我在相对上下文中调用它们。这是一个更好的例子:

class myClass{
    function test1()
    {
        if(method_exists("myClass", "test1"))
        {
            echo "Hi";
        }
    }
    function test2()
    {
        if(method_exists($this, "test2"))
        {
            echo "Hi";
        }
    }
    function test3()
    {
        if(method_exists(self, "test3"))
        {
            echo "Hi";
        }
    }
}
// Echos Hi
myClass::test1();
// Trys to use 'self' as a string instead of a constant
myClass::test3();
// Echos Hi
$obj = new myClass;
$obj->test2();

I need to be able to make test 3 echo Hi if the function exists, without needing to take it out of static context. Given the keyword for accessing the class should be 'self', as $this is for assigned classes.

如果该函数存在,我需要能够使 test 3 echo Hi,而无需将其从静态上下文中取出。鉴于访问类的关键字应该是“self”,因为 $this 用于分配的类。

回答by gapple

static::classis available since PHP 5.5, and will return the "Late Static Binding" class name:

static::class自 PHP 5.5 起可用,并将返回“ Late Static Binding”类名:

class myClass {
    public static function test()
    {
        echo static::class.'::test()';
    }
}

class subClass extends myClass {}

subClass::test() // should print "subClass::test()"

get_called_class()does the same, and was introduced in PHP 5.3

get_called_class()做同样的事情,并在 PHP 5.3 中引入

class myClass {
    public static function test()
    {
        echo get_called_class().'::test()';
    }
}

class subClass extends myClass {}

subClass::test() // should print "subClass::test()"


The get_class()function, which as of php 5.0.0 does not require any parameters if called within a class will return the name of the class in which the function was declared (e.g., the parent class):

get_class()函数,从 php 5.0.0 开始,如果在类中调用,则不需要任何参数,将返回声明该函数的类的名称(例如,父类):

class myClass {
    public static function test()
    {
        echo get_class().'::test()';
    }
}

class subClass extends myClass {}

subClass::test() // prints "myClass::test()"

The __CLASS__magic constantdoes the same [link].

__CLASS__魔术不断做同样[链接]。

class myClass {
    public static function test()
    {
        echo __CLASS__.'::test()';
    }
}

class subClass extends myClass {}

subClass::test() // prints "myClass::test()"

回答by hobodave

Update:

更新:

Ahh, apologies. I was temporarily blind :) You'll want to use the magic constant __CLASS__

啊,抱歉。我暂时失明了 :) 你会想要使用魔法常量 __CLASS__

e.g.

例如

if (method_exists(__CLASS__, "test3")) { echo "Hi"; }

回答by sxn

for all situations… the best usage would be…

对于所有情况……最好的用法是……

if method_exist(…) && is_callable(…)

For testing example:

对于测试示例:

class Foo {
  public function PublicMethod() {}
  private function PrivateMethod() {}
  public static function PublicStaticMethod() {}
  private static function PrivateStaticMethod() {}
}

$foo = new Foo();

$callbacks = array(
  array($foo, 'PublicMethod'),
  array($foo, 'PrivateMethod'),
  array($foo, 'PublicStaticMethod'),
  array($foo, 'PrivateStaticMethod'),
  array('Foo', 'PublicMethod'),
  array('Foo', 'PrivateMethod'),
  array('Foo', 'PublicStaticMethod'),
  array('Foo', 'PrivateStaticMethod'),
);

foreach ($callbacks as $callback) {
  var_dump($callback);
  var_dump(method_exists($callback[0], $callback[1])); // 0: object / class name, 1: method name
  var_dump(is_callable($callback));
  echo str_repeat('-', 40), "n";
}

Source here

来源在这里