Why is it not possible to return self in PHP?

15,739

Solution 1

I cannot give you a reason why other than the syntax itself isn't supported. It almost could work in PHP 5.3:

class Foo
{
  public static function A()
  {
    return __CLASS__;
  }

  public static function B() { }
}

$chain = Foo::A();
$chain::B();

If PHP would parse Foo::A()::B() then it would work.

Solution 2

Try return new static() or return new self():

class Calculator
{
    private static $_var = 0;

    public static function startFrom($var)
    {
        self::$_var = $var;
        return new static();
    }

    public static function add($var)
    {
        self::$_var += $var;
        return new static();
    }

    public static function sub($var)
    {
        self::$_var -= $var;
        return new static();
    }

    public static function get()
    {
        return self::$_var;
    }
}

This could be used for chain static methods:

echo Calculator::startFrom(10)
    ->add(5)
    ->sub(10)
    ->get(); // return 5

New self vs. new static

Solution 3

You can't return a 'self' because no OOP language I know allows to return a type as a type (don't know how to rephrase that). However everyone allows to returns an instance of a type. A static method is part of a class definition and it is callable as long as the application runs.

When doing OOP, you should use the static keyword very carefuly, as it's very easy to abuse it. If you want to chain methods then use an object. Static methods should be used only when no state is required and the function simply process an input and returns a result.

When chaining you have to maintain state and that's where you don't use static classes/methods at all (ok there are some cases but those are exceptions and it's not the case here).

Share:
15,739
js-coder
Author by

js-coder

I enjoy coding and like JavaScript, the open source movement and Unix.

Updated on July 01, 2022

Comments

  • js-coder
    js-coder almost 2 years

    In PHP it's not possible to return self to chain static methods. This limits the usage of static methods, because chaining is pretty useful and you have to use instances to chain methods.

    Are there any reasons why the PHP developers decided not to allow returning self? Or is it not possible to return self in OOP in general?

  • CodeCaster
    CodeCaster about 12 years
    Luckily PHP hasn't got any feelings, since it gets told a few million times a day to die().
  • Niklas B.
    Niklas B. about 12 years
    In Python or Ruby, return cls/self works just fine in class methods (because actually classes are first-class objects themselves in these and many other languages).
  • MikeSW
    MikeSW about 12 years
    I don't know python nor ruby :). However is still strange to me to return the static class as itself, only to save typing a few characters?!
  • Niklas B.
    Niklas B. about 12 years
    MikeSW: I didn't say it would be sensible to do that, but it should at least be possible in object-based languages (object-based as in everything is an object, including types).