0

I have got a warning in PhpStorm for a common use of static calls. I have:

class Test {
  public static function thisIsATest(){
     // do stuff
  }
}

Then, I have:

$className = 'Test';
$className::thisIsATest();

This is not an error, btw I have this in my PhpStorm: warning_phpstorm

Is there a way to handle this? At least, is it possible to just hide this warning?

5
  • How about using PHP 5.3.0 or greater. Before this version, your syntax isnt allowed. Test Versions herer: sandbox.onlinephpfunctions.com Commented Jan 12, 2017 at 16:32
  • Alternative you can do this: call_user_func(array('Test','thisIsATest')); Commented Jan 12, 2017 at 16:36
  • 1
    Unfortunately PhpStorm does not support such class referencing properly yet. Two options 1) Disable "Unknown method" inspection .. but this will affect all other places as well; 2) Suppress message for this line by placing /** @noinspection PhpUndefinedMethodInspection */ on a line just before $className::thisIsATest(); 3) Give a hand to IDE by providing better type hint for $className variable -- the one that IDE understands: /** @var Test $className */ before $className = 'Test'; line Commented Jan 12, 2017 at 17:08
  • Disabling this inspection is not really an option. I hoped that there were a way to tell Phpstorm inspector how to handle this. Using /** @var ... is an option, but that bothers me because it's not its really type. Commented Jan 13, 2017 at 9:53
  • Afterwards, they introduced this syntax in php5.3 to avoid using call_user_func functions (as far as i know). I'm going to downgrade the severity of this warning for now. Thank's! Commented Jan 13, 2017 at 9:55

1 Answer 1

2

There are some solutions, for this problem:

  1. change to a PHP Version >= 5.3.0

  2. use call_user_func(array('Test','thisIsATest')); in older PHP versions

  3. change code-inspection-behavior in phpstrom under project settings (also ensure phpstrom has the right PhpVersion set up for your current project)

  4. [Solution] Declare the variable with annotations in phpstrom in the right way!

Like:

  <?php
  /**
   * @var $className Test
   */
  $className::thisIsATest();

Now phpstrom know that $className is a instance of Test that has a method called thisIsATest, and no error hint pops up.

Sign up to request clarification or add additional context in comments.

5 Comments

In fact, my code works (i'm on a PHP5.6). It's just the code hilighter of Phpstorm which shows me a warning. I downgraded the severity for now. Thank's!
@dam660 Working with Phpstorm feels sometimes like having a million minions around :-) I have noted a little bit late that your Q was about phpstrom not the code itself. :)
@dam660 Check my answer again!
Oh! This works! This syntax is not part of the documentation. It says : @var [“Type”] [$element_name] [<description>]. Your proposal is to switch $element_name with type. What does it really means? Thank you!
@dam660 I some cases the second argument [$variablename] is optional, so maybe it also detects the first two arguments by itself, so the order isnt not really relevant. Also your link show the use within a class, but in my case it is used global. And in the end phpstorm does his interpretation. But, as long it worked ... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.