Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call static method from class inheriting Interface [duplicate]

Tags:

php

Possible Duplicate:
Referring to a static member of a subclass

Please have a look at the following code to understand my problem.

<?php

Interface IDoesSomething
{
    public static function returnSomething();
}

abstract class MiddleManClass implements IDoesSomething
{
    public static function doSomething()
    {
        return 1337 * self::returnSomething();
    }
}

class SomeClass extends MiddleManClass
{
    public static function returnSomething()
    {
        return 999;
    }
}

// and now, the vicious call
$foo = SomeClass::doSomething();

/**
 * results in a
 * PHP Fatal error:  Cannot call abstract method IDoesSomething::returnSomething()
 */
?>

Is there a way to force abstraction of returnSomething() while maintaining the possibility to call the function from a function defined in an abstract "middleman" class? Looks like a bottleneck of PHP to me.

like image 276
Daniel Avatar asked Dec 13 '22 06:12

Daniel


1 Answers

If you php version >= 5.3 then change

public static function doSomething()
{
    return 1337 * self::returnSomething();
}

to

public static function doSomething()
{
    return 1337 * static::returnSomething();
}
like image 148
xdazz Avatar answered May 17 '23 14:05

xdazz