Search code examples
phpstorm

PHPStorm Intellisense Does Not Recognize Constants Defined in Class


Is there a way to get PhpStorm intellisense to pick up these dynamically defined constants? Given the code below, PhpStorm gives the "Undefined constant SAMPLE_CONSTANT_THAT_WAS_DYNAMICALLY_DEFINED" error message.

class ExampleConfiguration
{
   private $configurationMapping;
   ...
   public function DefineConfigConstants()
   {
      foreach ($this->configurationMapping as $key => $value)
         define($key, $value);
   }
}

class ExampleClass
{
   public function Test()
   {
      print SAMPLE_CONSTANT_THAT_WAS_DYNAMICALLY_DEFINED;
   }
}

This issue can be tracked here: https://youtrack.jetbrains.com/issue/WI-11390, what I'm looking for is suggestions for workarounds.


Solution

  • IDE needs to know about such constants in order to not to complain about them. This means that they have to be defined in "normal" way (actual values do not matter, as long as they are not used for file names/paths in include/require statements).

    Suggestion: write custom script that create such myconstants.php file where they will be defined in a normal way (since all such constants defined by users and stored in DB, you have to fetch them from DB yourself) .. and run this script (to update generated file) before working with the code in PhpStorm.