不能为usort做OOP回调函数 [英] Cannot do OOP Callback function for usort

查看:54
本文介绍了不能为usort做OOP回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了几十篇关于使用类成员作为回调函数的讨论,但没有一个例子解决在我看来是明显的 OOP 设计,基于使用其他 OOP 语言,如 C++ 和 Java.

I have read through a few dozen discussion of the use of class members as callback functions but none of the examples address what seems to me to be the obvious OOP design, based upon working with other OOP languages such as C++ and Java.

我在类中定义了一个比较方法:

I have defined a comparison method within the class:

class TestClass {
private $aField; // integer value
function _construct($value)
{
        $this->aField   = $value;
}       // TestClass::_construct

function compare($other)
{
    if ($other instanceof TestClass)
    {           // comparing two instances
    return $this->afield - $other->afield;
    }           // comparing two events
    else
    throw new Exception("parameter is not instance of TestClass");
}       // TestClass::compare
}       // class TestClass

$instances  = array(new TestClass(5),new TestClass(3));
// the following fails because:
// 1. $this is not defined outside a class member
// 2. compare does not take two parameters
usort($instances, array($this, 'compare'));

// the following kluge works around this
function order($this, $that) { return $this->compare($that); }
usort($instances, 'order');

我可以在这个论坛和 PHP 文档中找到的所有示例都不完整,因为它们没有显示调用 usort 的上下文.现在我可以通过使比较函数成为具有两个参数的静态函数并将其作为可调用数组('TestClass','compare')来调用它来解决这个问题,但是将比较方法定义为静态函数并不直观班上.基于 30 多年的 OOP 个人经验,静态函数在大多数情况下是糟糕的类设计的结果.特别是静态方法打败了多态性.

All of the examples I can find on this forum and in the PHP documentation are not complete in that they do not show the context of the call to usort. Now I can get around this by making the comparison function a static function with two parameters and invoke it as a callable by array('TestClass','compare'), but it is not intuitive to define a comparison method as a static function of the class. Based upon over 30 years of personal experience in OOP static functions are in most cases a result of poor class design. In particular static methods defeat polymorphism.

我正在寻找一种类似于 Java 的排序函数,它利用了 Comparable 接口.我看到早在 2010 年就有一些关于定义 Comparable 接口的讨论,但该讨论仅与比较运算符有关,并且使 PHP 纯粹主义者感到不舒服,因为它重载了运算符.然而,Java Comparable 接口重载运算符,因为除了 String 类的明显例外,Java 不支持重载运算符.在 Java 中实现 Comparable 接口使您能够在 SortedMaps 中使用该类并对 Collection 中的实例进行排序,但是如果您希望比较两个对象,则必须显式调用 compareTo 方法.我还看到有一个基本上没有记录的 compare_objects 方法,它已经覆盖了比较运算符的默认实现.换句话说,尽管纯粹主义者反对,PHP 已经允许您重载类的比较运算符.然而,当要求对对象进行排序时,PHP 排序函数是否使用 compare_objects 没有记录.

What I am looking for is a sort function like that of Java, which exploits the Comparable interface. I see that there has been some discussion dating back to 2010 about defining a Comparable interface, but that discussion is only in connection with the comparison operators, and makes PHP purists uncomfortable because it overloads operators. However the Java Comparable interface does not overload operators since, with the glaring exception of the String class, Java does not support overloading operators. Implementing the Comparable interface in Java enables you to use the class in SortedMaps and to sort instances within a Collection, but if you wish to compare two objects you must explicitly call the compareTo method. I also see that there is an essentially undocumented compare_objects method which already overrides the default implementation of the comparison operators. In other words despite the purists' objections PHP already permits you to overload the comparison operators for a class. However it is undocumented whether or not the PHP sort function uses compare_objects when it is asked to sort objects.

推荐答案

你在课堂外调用 $this.

You're calling $this outside of your class.

$obj = new TestClass();
$sorted = usort($instances, array($obj, 'compare'));

你也可以这样做(没有测试代码)

You could also do something like this (didn't test the code)

class TestClass() {
    function compareWith($other) {
        $arr = array($this, $other);
        usort($arr, function($that, $other) {
            if ($other instanceof TestClass)
            {           // comparing two instances
            return $other->afield - $that->afield;
            }           // comparing two events
            else
                throw new Exception("parameter is not instance of TestClass");
        });
        return $arr;
    }
}

$instance1  = new TestClass();
$instance2  = new TestClass();
$sorted = $instance1->compareWith($instance2);

这篇关于不能为usort做OOP回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆