我需要在$variable
返回文档中添加php
例如,我有这个功能:
/**
* @return \Panel\Model\{$Service}
*/
public function getService($Service){
// I call service `foo` from Model folder
}
我看到这篇文章:What's the meaning of @var in php comments但它没有关于如何做的信息,也研究@var-phpdoc但这对我没什么用。
如果你问我为什么要这样做,因为我想在phpStorm
上使用Ctrl+Click
$this->getService('foo')->bar()
优势
提前致谢
答案 0 :(得分:2)
正如LazyOne在评论中所说,PHP接口应该可以解决您的问题。你不能在PHPDoc格式的注释中使用变量。当然,如果你使用像PHPStorm这样的IDE和一个允许在PHPDoc注释中使用变量的插件,问题就会自行解决。什么,当其他开发人员,谁不使用PHPStorm或相关的插件,想在同一个项目中工作?在我看来,你应该使用PHP本机功能来解决你的问题。
这是一个如何使用接口的简短示例。
declare('strict_types=1');
namespace Application\Model;
interface ModelInterface
{
public function getFoo() : string;
public function setFoo() : ModelInterface;
}
您现在唯一需要做的就是将此界面与您的模型一起使用,如下例所示。
declare('strict_types=1');
namespace Application\Model;
class FooModel implements ModelInterface
{
protected $foo = '';
public function getFoo() : string
{
return $this->foo;
}
public function setFoo(string $foo) : ModelInterface
{
$this->foo = $foo;
return $this;
}
}
如您所见,FooModel
类实现了ModelInterface
接口。因此,您必须使用模型类中接口中声明的方法。这意味着,您的getService
方法可能类似于以下示例。
/**
* Some getter function to get a model
* @return \Application\Model\ModelInterface
*/
public function getService($service) : ModelInterface
{
return $service->get(\Application\Model\Foo::class);
}
您的IDE现在知道返回的类可以使用哪些方法。它允许您使用链接和更多功能。在键入IDE时,现在应该知道,返回的类可以使用getFoo
和setFoo
方法。此外,setFoo
方法可以为诸如此类的调用提供舒适的链接。
// variable contains the string 'foo'
// your ide knows all methods
$fooString = $this->getService($serviceLocator)->setFoo('foo')->getFoo();
答案 1 :(得分:0)
您使用的是Symfony吗?然后你可以使用Symfony plugin来解决这个问题。对于其他框架,应该有类似的解决方案。如果您使用自己的框架,则需要自己编写这样的插件,否则PhpStorm无法解析给定的类。
答案 2 :(得分:0)
我认为你要找的是phpdoc。
https://docs.phpdoc.org/guides/docblocks.html
/**
* @param string $Service This is the description.
* @return \Panel\Model\{$Service}
*/
public function getService($Service){
// I call service `foo` from Model folder
}