1

I want to import data from a .json file into a PHP file and dynamically, by using variable variables and a foreach loop, define new variables. However, PhpStorm doesn't recognize my variables and give me Undefined variable warnings. What to do?

Consider a file data.json with some data, and it exists and is readable. The main code is something like this:

foreach (json_decode(file_get_contents("data.json"), true) as $varName => $value)
    $$varName = $value;

Note: I don't want neither to use PHPDoc, nor to disable "undefined variable" inspection (thanks LazyOne).

UPDATE: Thanks to LazyOne's comment, I'll use PHP files instead of JSON ones to save data.

MAChitgarha
  • 3,728
  • 2
  • 33
  • 40
  • Can you please add your code so far – Nigel Ren Jun 17 '18 at 17:39
  • @NigelRen Yep. Added. – MAChitgarha Jun 17 '18 at 17:46
  • 1
    Is there any reason you don't want to keep it as an array, it's much more flexible and less prone to problems. – Nigel Ren Jun 17 '18 at 17:51
  • @NigelRen I want to save it as a separate file, i.e. data should be separated from code. What's your recommendation? – MAChitgarha Jun 17 '18 at 17:58
  • @NigelRen The problem is not with having variables, but with IDE. The variables work properly and they work as expects. But IDE cannot recognize those, when I use them somewhere in my code, it produces warnings. – MAChitgarha Jun 17 '18 at 18:09
  • Is `stdObject` iterable? do you rally want `json_decode(file_get_contents("data.json"),true)`? Using `true` as the 2nd argument will return an array instead of a stdObject. – Mr Glass Jun 17 '18 at 18:12
  • 1
    *"I don't want to use PHPDoc"* Well ... then only other option is to disable that "unknown variable" inspection. – LazyOne Jun 17 '18 at 18:14
  • @LazyOne But it's not what I'm looking for. – MAChitgarha Jun 17 '18 at 18:16
  • 2
    Your IDE won't know anything about variable variables. It would have to understand your JSON and how you interpret it, which is beyond the scope of what it can deal with. – Nigel Ren Jun 17 '18 at 18:25
  • 1
    You have no other choice: IDE has no idea where those variables come from and why they should be "ignored" by that inspection. You want to use "magic" -- be ready to face the consequence -- static analysis that IDE does right now does not allow to analyze such dynamic code. – LazyOne Jun 17 '18 at 18:38
  • @LazyOne Do you recommend to use PHP files to store data instead of JSON files to solve this problem? – MAChitgarha Jun 17 '18 at 18:46
  • 2
    I have no idea on what data you have got there and how else it is used / where it comes from etc. From speed point of view -- keep it as PHP array / constants / variables. If data comes from some external source / another language or service -- better use what you are using (JSON/YAML/XML). But you will have to waste some time for data parsing/conversion into PHP accessible form each time. Caching will help though. – LazyOne Jun 17 '18 at 18:56
  • Thank you all, specially LazyOne and Nigel Red. – MAChitgarha Jun 17 '18 at 19:14

0 Answers0