Reputation: 3
Can somebody tell me why the autocompletion doesn't work when I'm using a getInstance()
method instead of new ClassName?
Following is the getInstance()
method of the class:
// Define The Namespace For Our Library
namespace JUnstoppable;
class JUnstoppable
{
// Instance Of The Class
protected static $instance = array ();
public static function getInstance ($forPlatformName = 'joomla')
{
$forPlatformName = strtolower($forPlatformName);
if (!isset(static::$instance[$forPlatformName]))
{
static::$instance[$forPlatformName] = new \JUnstoppable\JUnstoppable($forPlatformName);
}
return static::$instance[$forPlatformName];
}
public function __construct ($platformName = 'joomla')
{
}
public function doExecute ($test = 'lalala')
{
return $test;
}
}
Upvotes: 0
Views: 85
Reputation: 165118
Can somebody tell me why the autocompletion doesn't work when I'm using a
getInstance()
method instead of new ClassName?
That's because IDE does not know what can be inside your $instance
static property and therefore it cannot figure out what getInstance()
returns. From IDE point of view it's just plain array (elements of any types) and not an array of JUnstoppable
instances.
You can place caret on $test
and invoke View | Quick Documentation
to see what IDE knows about that variable. If it does not say JUnstoppable
there then no wonders.
Just add proper type hint for return value of getInstance()
method via PHPDoc's @return
tag:
/**
* My super method.
*
* @param string $forPlatformName Optional parameter description
* @return JUnstoppable
*/
public static function getInstance ($forPlatformName = 'joomla')
You can specify concrete class (JUnstoppable
in this case) .. or static
if this method will be used by child classes as well and they will return different instances.
Alternatively (or better say: in addition) you can typehint $instance
property which IDE will use to figure out what getInstance()
method returns:
/** @var JUnstoppable[] Instance Of The Class */
protected static $instance = array ();
Upvotes: 1