PHP docBlock @return className

How to create a PHP DocBlock with @return indicating the return of the class. It’s pretty simple right now by doing

/** * This returns an object of the "User" class * @return User */ public function getUser() { return $this->user; } 

I use this to get intellisense via my IDE for these return values. (in my case Netbeans)

However, I have a class that returns a class based on the name of the variable. (eg:)

 /** * This returns an object of the $param * @param String $className * @return ??? */ public function getSomeObject($className) { return new $className(); } 

and I'm trying to create intellisense for this, but I'm not sure if this is really possible.

For example, when I call

 $someClass = new MyClass(); $var = $someClass->getSomeObject('Address'); 

I would like my IDE to show me intellisense for the $ var variable (which will contain the address object)

+4
source share
2 answers

It makes sense to mention that the method returns an object as such:

 /** * [...] * @return object */ 

In the end, some details:

 * @return object Object of class $className 

See the docs for @return .

+3
source

If you cannot list all possible return types,

 @return User|Address|Sandwich|Coiture 

and , the IDE can allow autocomplete to combine all the methods / properties from the entire list of classes, then I do not see the possibility in this.

+1
source

Source: https://habr.com/ru/post/1402001/


All Articles