skip to Main Content

When I call something like:

$class_name = 'AppModelsUser';
$class_name::create($attributes);

PhpStorm shows an inspection warning "Method ‘create’ not found in string".

I don’t want to switch off this inspection, as it’s very useful in other cases. Is there another way to avoid the warning, using annotations or something else?

2

Answers


  1. Chosen as BEST ANSWER

    I've found another way, maybe someone will use it:

    call_user_func($class . '::create', $attributes);
    

    Not ideal, but works fine


  2. The warning you’re seeing in PhpStorm is likely due to the fact that PhpStorm is statically analyzing the code, and when you use a string to represent a class name, PhpStorm can’t infer the actual class and, therefore, doesn’t recognize the methods associated with that class.

    To mitigate this, you can use PHPDoc annotations:

    /** @var AppModelsUser $class */
    $class = 'AppModelsUser';
    
    $class::create($attributes);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search