Hinting type abstract singleton class

How would one Hint type static monochromatic return method in an abstract class that returns instances of extensible named classes?

For example, consider the following code:

<?php

abstract class Foo {

    /** @return Foo */
    public function init() {
        static $instance;

        if ( is_null($instance) ) {
            $class = get_called_class();
            $instance = new $class();
        }

        return $instance;
    }

}

class Bar extends Foo {

    public $name = "Bar name";

}

class Baz extends Foo {

    public $age = 42;

}

      

I intend to use tools like PhpStorm to understand what Bar::init()

type object returns Bar

and what Baz::init()

type object returns Baz

. This way, for example, objects created from a method Baz::init()

would automatically complete the property name

, but not the property age

.

Obviously, the current hint type is @return Foo

incorrect, since the method will never return an instance of an object of the abstract class.

+3


source to share


2 answers


So @return static

will work in this case for PHPStorm. This is the easiest option and will provide what you are looking for.

Optionally, you can use annotation @method

on the class, although this is very manual and needs to be done for each class. There is another weird thing with this method in PHPStorm where if you go to the method init()

(ctrl + click or w / e) it will go to that annotation first. However, it looks like this:

/**
 * @method Bar init()
 */
class Baz extends Foo 
{
}

      



Where is the choice as the ultimate resort - and I really don't think you'll need it, but its here for completeness. Extend the method and add a return annotation just like a normal method.

/**
 * @return Baz
 */
public function init() 
{
    return parent::init();
}

      

+3


source


You can try this:

abstract class Foo {

    /** @return static */
   public function init() {
       static $instance;

       if ( is_null($instance) ) {
           $class = get_called_class();
           $instance = new $class();
       } 

        return $instance;
    }

}

      



This will probably require a PhpStorm installation with hints like PHP 5.3+.

+1


source







All Articles