Home » Php » php – Tell intelephense that the function is valid

php – Tell intelephense that the function is valid

Posted by: admin February 25, 2020 Leave a comment

Questions:

Consider the following class structures:

trait MyTrait {
    public function definedFunction() {

    }
}

interface MyInterface {

}

class A {
     public function test() {
         if ($this instanceof MyInterface) {
              // my intelisense tells me that the definedFunction() is not defined
              $this->definedFunction();
         }
     }
}

class B extends A implements MyInterface {
     use MyTrait;
}

Could I tell somehow to my intellisense (with doc blocks or anything), that in that specific situation, definedFunction is indeed defined? The above code works, despite intelephense complaining about it, could I disable that complaint with a docblock or something?

On the other hand, is there any other way to restructure my code for intellisense to know about this method? Basically, am I doing something wrong here with this structure?

What I want to achieve is, the following: class A is a template class, which is extended by a bunch of other classes. Some of these other classes, should be able to use some common methods, but not all of them. Because I can not extend multiple classes in PHP, I tried resolving this with traits. If I happen upon one of this kind of classes, I implement an interface with it (to group it somehow for later accessibility), and tell it to use the defined trait, to get all the added functionalities. The function that I call test in the above example, is a function that ALL of the classes that extend class A must be able to access, but when a class implements MyInterface, that function should get some added functionalities. This is why I didn’t put that into the trait I am defining.

How to&Answers: