Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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 Usort,在调用静态类中使用回调_Php_Oop_Usort_Static Classes - Fatal编程技术网

在父静态类中使用PHP Usort,在调用静态类中使用回调

在父静态类中使用PHP Usort,在调用静态类中使用回调,php,oop,usort,static-classes,Php,Oop,Usort,Static Classes,我有一个作为单例使用的“静态类”(因为不需要有多个实例),还有其他扩展它并以相同方式使用的类 在处理过程中的某个时刻,扩展类将调用parent::doThis(),doThis最终将执行usort usort的回调应该在调用类中,因为每个类处理排序的方式都不同。类似于“class::method()”的内容是否可以作为usort回调的字符串,如果可以,父类是否有办法知道哪个类调用了它,而无需我将其作为参数传递,这样它就可以为usort命名调用类的回调 class parentClass {

我有一个作为单例使用的“静态类”(因为不需要有多个实例),还有其他扩展它并以相同方式使用的类

在处理过程中的某个时刻,扩展类将调用parent::doThis(),doThis最终将执行usort

usort的回调应该在调用类中,因为每个类处理排序的方式都不同。类似于“class::method()”的内容是否可以作为usort回调的字符串,如果可以,父类是否有办法知道哪个类调用了它,而无需我将其作为参数传递,这样它就可以为usort命名调用类的回调

class parentClass {
  protected static function doThis($data) {
    // do stuff, then
    usort($data, "myCallingClass::cmp()"
  }
}
基于父级确定myCallingClass是什么的某种方法,或者它需要是什么

class parentClass {
  protected static function doThis($data, $calling_class) {
    // do stuff, then
    usort($data, $calling_class . "::cmp()"
  }
}

我认为你应该能够做到这一点:


您可以将
静态函数名(){return“myCallingClass”}
添加到每个具有自己名称的类中。那你只需要打个电话

usort($data, static::name() . "::cmp()");

static
modifier向您保证,如果继承类有一个方法,则将从继承类调用该方法。

一个简单的解决方案是结合使用一个众所周知的回调名称:

一个不那么简单但正确的解决方案是忘记所有这些“静态类”和“单例”的东西,并按预期的方式使用对象:

class Numbers
{
    function sort() {
        $things = [1,2,3,4,5];
        usort($things, [$this, 'cmp']);
        print join(' ', $things);
    }
}

class Mod2 extends Numbers {
    function cmp($a, $b) {
        return $a % 2 - $b % 2;
    }
}

(new Mod2)->sort(); // 4 2 5 3 1

这两种解决方案都应该有效,但是如果你需要放置不同的回调,那么就使用#2,这样你就可以在回调之间切换。问题出了什么问题?我看到它被否决了。这样很好,但还没有在php中实现。@georg什么没有实现?后期静态绑定从5.3(约5年)开始在PHP中使用。它被引用,只是尝试过而已。在父类中,我有公共静态函数p(){print'hello';}和公共静态函数test(){static::p();},然后在扩展类中有公共静态函数p(){print'debye';},在扩展类中调用self::test()会导致“再见”;还没试过用usort。。。这就是没有实现的吗?如果usort不直接接受静态可调用函数,您可能需要将其封装在一个闭包中:
usort($data,function($a,$b){return(static::cmp($a,$b));}
class Numbers
{
    static function sort() {
        $things = [1,2,3,4,5];
        usort($things, [get_called_class(), 'cmp']);
        print join(' ', $things);
    }
}

class Mod2 extends Numbers {
    static function cmp($a, $b) {
        return $a % 2 - $b % 2;
    }
}

Mod2::sort(); // 4 2 5 3 1
class Numbers
{
    function sort() {
        $things = [1,2,3,4,5];
        usort($things, [$this, 'cmp']);
        print join(' ', $things);
    }
}

class Mod2 extends Numbers {
    function cmp($a, $b) {
        return $a % 2 - $b % 2;
    }
}

(new Mod2)->sort(); // 4 2 5 3 1