Code: Select all
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 $other->afield - $this->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));
$sorted = usort($instances, array($this, 'compare'));
The definition of a callback function in PHP essentially precludes it being a normal class method. Yes there is a situation where you can call a normal class method from another class method, but you cannot perform a sort using a comparison method like the above from outside the class, where you do not have access to $this. Even if you do have a class method it might as well have "static" defined because the comparison method must have two parameters to define what it is comparing, it cannot, as above, compare the current instance to another instance.
I have long felt that the use of a static method indicates a poor class design because static methods defeat polymorphism.
It also seems really weird coming from any other language to see a function referred to by its character string name rather than by an object instance. To me this seems to be exposing the internal implementation of PHP in a way that should be unnecessary.
I understand that this is just the way PHP is, but I dislike being forced to add a wrapper function around a comparison method that is defined in the way that comparison methods are defined in almost every other language just to get it accepted syntactically.
Is there any forum for discussions of PHP evolution where I could discuss this issue.