Note that there are some explanatory texts on larger screens.

plurals
  1. POInclude vs Extend for data access
    primarykey
    data
    text
    <p>This is a design question that has irked me because deep down I know I'm doing it wrong. This is not necessarily just a PHP problem, but I'm working on a PHP project now so that's where I'm coming from. I'll just give you the scenario: I have a JSON generation class that is used by a data access script. Basically it's the go-to URL for a website that instantiates the JSONGen class, which in turn looks at a static file and pulls the data that was requested, and sends it back as a JSON object. The file that contains the raw data has it all in array format to allow for easier reading and updating (so one doesn't have to sift through raw JSON data and risk syntax error, etc). My question is in the accessing of that file. What I did was to place a require([json file]) outside the class definition. Everything in the [json file] is assigned to one $JSON object. In the class constructor, I do:</p> <pre><code>global $JSON; $this-&gt;JSON = $JSON; </code></pre> <p>(Where $this->JSON is a private class variable).</p> <p>This gives me access to the JSON from anywhere in the class, which is lovely. It feels hacky, but I can't find anything anywhere that addresses this specific type of approach.</p> <p>The problem is that once somebody decides to move the class or whatever, the only way they have of understanding the dependency of this file is in that there's a require statement in the header. Is that just how things work? Or is there a better way to do this?</p> <p><strong>Should I use extend and put the JSON into a class (THAT sounds really stupid to me), or continue forward with my include? Or should I never, ever include a file in a class like this and rather pass it to the constructor or some other access method?</strong></p> <p>This specific instance is a small project, and it just doesn't make sense to set up a DB for it. We can add to the file, modify the config, and the whole site updates.... so please don't tell me to switch to a DB- I'm looking for strategies for this specific problem in a more general sense.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload