Magic method for when a class instance is called like a function?
<p>What I want to do is create a <code>Comparable</code> class, similar to <code>IComparable</code> in .NET, such that you can instantiate it like so:</p>

<pre><code>$cmp = new MultiArrayCompare(2);
</code></pre>

<p>And then you can sort an array via:</p>

<pre><code>usort($myArray, $cmp);
</code></pre>

<p>And it will sort an array of arrays on the 2nd index. To do that, I imagine <code>usort</code> would try to call <code>$cmp</code> like a function, so I'd have to override that behaviour somehow. It doesn't look like <code>__call</code> does what I want (thought for a minute it was like Python's <a href="http://docs.python.org/reference/datamodel.html#object.__call__" rel="nofollow"><code>__call__</code></a>).</p>

<p>If this isn't possible... is there another nice way to create a general solution to this problem? Where you can create a user-defined sorting function but give pass it some value ("2" in this case)?</p>

<hr>

<p>Using <code>__invoke</code> I was able to create these classes:</p>

<pre><code>abstract class Comparable {
    abstract function Compare($a, $b);

    function __invoke($a, $b) {
        return $this-&gt;Compare($a, $b);
    }
}

class ArrayCompare extends Comparable {
    private $key;

    function __construct($key) {
        $this-&gt;key = $key;  
    }

    function Compare($a, $b) {
        if($a[$this-&gt;key] == $b[$this-&gt;key]) return 0;
        return $a[$this-&gt;key] &lt; $b[$this-&gt;key] ? -1 : 1;
    }
}

class ArrayCaseCompare extends Comparable {
    private $key;

    function __construct($key) {
        $this-&gt;key = $key;  
    }

    function Compare($a, $b) {
        return strcasecmp($a[$this-&gt;key], $b[$this-&gt;key]);
    }
}
</code></pre>

<p>Which I can use to sort an array of arrays:</p>

<pre><code>$arr = array(
    array(1,2,3),
    array(2,3,4),
    array(3,2,4),
)

usort($arr,new ArrayCompare(1));
</code></pre>
