Type hinting for class returned by DAO
Hello,
I work on a project that has a custom build MVC framework. Models are retrieved using a custom build Data Access Object.
The retrieval of a model works like this:$user = DAO::get('User', $id)
This will return the user model for the given id. The ‘problem’ is that PHPStorm doesn't know what kind of object $user contains. Type declaration in the method @return isn't possible because the return is dynamically based on the first parameter of the method.
What I do to get hinting is define the class manually like this:
/** @var User $user */
$user = DAO::get('User', $id)
This works fine but I have to manually add that line everytime I retrieve a model. Is there anyway I could make PhpStorm understand that the return type of the DAO::get method is the name of the class that is given as string as first parameter?
Hope anyone can help me!
请先登录再写评论。
Hi there,
It's doable with Advanced Metadata. I'm just not super sure if it will work great with the static methods (
::get()
), it works fine with class methods→get()
.Just go to PhpStorm help and type “advanced metadata” in the search box.
I will post a direct link separately (since having a link requires moderator approval first, even if it's a link to their own documentation).
Start from the start so you know how it all works. The link below points to a specific functionality.
https://www.jetbrains.com/help/phpstorm/ide-advanced-metadata.html#map
Damn, that works like a charm!
FYI:
Thanks for the help!