Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
Type Hinting abstract class singleton

How would one Type Hint a static singleton-returning method on an abstract class, which returns instances of the extending called classes?

For example, let's look at 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;

}

My intention is for tools such as PhpStorm to understand that Bar::init() returns an object of type Bar and that Baz::init() returns an object of type Baz. Thus, for example, objects created from the Baz::init() method would auto-complete the name property but not the age property.

Obviously the current type hint @return Foo is wrong as the method will never return an object instance of an abstract class.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

So @return static will work in this case for PHPStorm. It is the easiest option and will provide what you are looking for.

Optionally you can use the @method annotation against the class although this is very manual and needs to be done for each class. There is another strange thing with this method in PHPStorm where if you navigate to the init() method (ctrl+click or w/e) it will navigate to this annotation first. However this is how that looks:

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

Where optionally as a final resort--and I really don't think you will need it but its here for completeness. Extend the method and add your return annotation as you would a normal method.

/**
 * @return Baz
 */
public function init() 
{
    return parent::init();
}
about 4 years ago · Santiago Trujillo Report

0

You could 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 PhpStorm to be set up with PHP 5.3+ type hints to work.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!