在 IDE 自动完成中添加 PHP 动态方法
Add PHP Dynamic Methods in IDE Autocomplete
我需要在 PHP class 中创建一些动态方法。
使用这个 class:
class SampleClassWithDynamicMethod
{
public function __call($methodName, $values)
{
if(!method_exists($this, $methodName)){
// Do something...
return "You called $methodName!";
}
}
$sample = new SampleClassWithDynamicMethod();
echo $sample->test();
// You called test!
echo $sample->anotherTest();
// You called anotherTest!
echo $sample->moreTest();
// You called moreTest!
效果很好。但是我怎样才能让 IDE 知道这个 class 有这些名称为这些动态方法:test()
、anotherTest()
和 moreTest()
?
您可以使用 PHP DocBlocks。这些由主要 PHP IDEs.
支持
具体来说,@method
注释。检查 docs.
使用文档中的示例:
/**
* @method string getString()
* @method void setInteger(integer $integer)
* @method setString(integer $integer)
* @method static string staticGetter()
*/
class Child extends Parent
{
// <...>
}
这将声明可以执行以下任何操作,这将被 IDE 识别并提供用于自动完成(显然,假设这些方法已以某种方式实现):
$child = new Child();
$child->setInteger(10);
$child->setString(2);
echo $child->getString();
// 2
$string = Child::staticGetter();
我需要在 PHP class 中创建一些动态方法。
使用这个 class:
class SampleClassWithDynamicMethod
{
public function __call($methodName, $values)
{
if(!method_exists($this, $methodName)){
// Do something...
return "You called $methodName!";
}
}
$sample = new SampleClassWithDynamicMethod();
echo $sample->test();
// You called test!
echo $sample->anotherTest();
// You called anotherTest!
echo $sample->moreTest();
// You called moreTest!
效果很好。但是我怎样才能让 IDE 知道这个 class 有这些名称为这些动态方法:test()
、anotherTest()
和 moreTest()
?
您可以使用 PHP DocBlocks。这些由主要 PHP IDEs.
支持具体来说,@method
注释。检查 docs.
使用文档中的示例:
/**
* @method string getString()
* @method void setInteger(integer $integer)
* @method setString(integer $integer)
* @method static string staticGetter()
*/
class Child extends Parent
{
// <...>
}
这将声明可以执行以下任何操作,这将被 IDE 识别并提供用于自动完成(显然,假设这些方法已以某种方式实现):
$child = new Child();
$child->setInteger(10);
$child->setString(2);
echo $child->getString();
// 2
$string = Child::staticGetter();