简体   繁体   English

如何在php中的类内部引用静态函数?

[英]How to reference a static function inside class in php?

I'am trying to make a reference to a static function inside a class: 我正在尝试在类内引用静态函数:

class Test {

    function __construct() {

        $this->fn1 = self::fn2;


    }

    public static function fn2() {

    }


}

then i get this error: 然后我得到这个错误:

Undefined class constant 'fn2'

why? 为什么?

You have defined a static function : 您已经定义了一个静态函数

Test {
    function__construct()
    {
        $this->fn1 = self::fn2();
    }

    public static function fn2()
    {

    }
}

Updated 更新

If you want to assign a function to a variable, it is best to do this with annonymous aka lambda functions since they are first class citizens and may be freely passed, returned and assigned. 如果要为变量分配函数,则最好使用匿名又名lambda函数来执行此操作,因为它们是一等公民,并且可以自由传递,返回和分配。 PHP is not unique in dealing with static method references in this fashion as JAVA implements them similarly: 在以这种方式处理静态方法引用时,PHP并不是唯一的,因为JAVA类似地实现了它们:

Method references ... are compact, easy-to-read lambda expressions for methods that already have a name. 方法引用...是已经有名称的方法的紧凑,易于阅读的lambda表达式。

You may create an anonymous function based on a callable in PHP, and so the OP may wish to do as follows, which PHP 7.1.10 or higher supports: 您可以基于PHP中的callable创建一个匿名函数,因此OP可能希望执行以下操作,PHP 7.1.10或更高版本支持:

<?php
class Test {
    public static function fn2() {
         return __METHOD__;
    }
    public static function getClosure (){
        return Closure::fromCallable(["Test","fn2"]);
    }
}


echo Test::getClosure()(),"\n";

See live code here 在此处查看实时代码

In this example an anonymous function is created and returned by the static getClosure method. 在此示例中,匿名函数由静态getClosure方法创建并返回。 When one invokes this method, then it returns the closure whose content is the same as static method fn2. 当调用此方法时,它将返回闭包,其内容与静态方法fn2相同。 Next, the returned closure gets invoked which causes the name of static method fn2 to display. 接下来,返回的闭包被调用,这将导致显示静态方法fn2的名称。

For more info re closures from callables, see the Manual and the RFC . 有关可调用对象的更多信息,请参见《 手册》和《 RFC》

With PHP 7 on up, you may create a complex callable . 随着PHP 7的启动,您可以创建一个复杂的callable In the code below the complex callable is an invocable array: 在下面的代码中,复杂的callable是一个可调用的数组:

<?php
class foo 
{
    public static function test()
    {   
        return [__CLASS__, 'fn2'];
    }   

    public static function fn2()
    {   
        echo __METHOD__;
    }   
}

echo foo::test()();

See live code . 查看实时代码

Note: Starting with PHP 7.0.23 you could create a complex callable using a string containing the class and method names separated by the double colon aka paaamayim nekudotayim; 注意:从PHP 7.0.23开始,您可以使用包含以双冒号aka paaamayim nekudotayim分隔的类和方法名称的字符串创建一个复杂的可调用对象。 see here . 这里

A solution that has broader PHP support is as follows: 具有更广泛的PHP支持的解决方案如下:

<?php

class Test {

    public static function fn2() {
           return __METHOD__;
    }
    public static function tryme(){
      return call_user_func(["Test","fn2"]);
    }

}
// return closure and execute it
echo Test::tryme();

See live code 查看实时代码

Not sure if this is what you want, but at least this might give you a hint: 不知道这是否是您想要的,但是至少这可能会给您提示:

<?php

class Test {

    function __construct() {

        $this->fn = function(){
             return self::realFn();
        };

    }

    public function callFn (){
        $fn = $this->fn ;//yes, assigning to a var first is needed. You could also use call_user_func
        $fn();
    }
    public static function realFn() {
        echo 'blah';
    }


}

$x = new Test();

$x->callFn();

You can test it here: https://3v4l.org/KVohi 您可以在这里进行测试: https//3v4l.org/KVohi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM