Reputation: 447
I need to make some dynamic methods in my PHP class.
Using this 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!
It works well. But how can I let the IDE know this class has these dynamic methods with these names: test()
, anotherTest()
and moreTest()
?
Upvotes: 1
Views: 594
Reputation: 47380
You can use PHP DocBlocks. These are supported by major PHP IDEs.
Specifically, the @method
annotation. Check the docs.
Using the example from the docs:
/**
* @method string getString()
* @method void setInteger(integer $integer)
* @method setString(integer $integer)
* @method static string staticGetter()
*/
class Child extends Parent
{
// <...>
}
This would declare that could could do any of the following, which would be recognized by the IDE and offered for auto-completion (obviously, assuming the methods have been implemented in some way):
$child = new Child();
$child->setInteger(10);
$child->setString(2);
echo $child->getString();
// 2
$string = Child::staticGetter();
Upvotes: 1