Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a config file from within a php class
    primarykey
    data
    text
    <p>I am kind of new to PHP OOP, but I have a fairly decent understanding of the concepts behind OO. I want to have a config file that has general application data that can be used from throughout the entire application. Pretty normal but I am not exactly sure how to do this. I don't want to create a class and then have require that class, extend it and or require the config file in every class. My config file looks something like this:</p> <pre><code>&lt;?php $configs = array( 'pagination' =&gt; 20, 'siteTitle' =&gt; 'Test site', 'description' =&gt; 'This is a test description', 'debug' =&gt; true ); ?&gt; </code></pre> <p>The only thing that I can think of to do for this is the following:</p> <pre><code>&lt;?php class user { public function __construct() { require 'config.php'; if(configs['debug']) { echo 'Debugging mode'; } } } ?&gt; </code></pre> <p>The problem I see with this method is that I would have to manually include this config file in every class I want to use which seems redundant. Ideally I would like to include the file in the absolute root path and then be able to use any of the values from within any class however if you simply require a file outside of the class the class will not have access to the values. I also don't want to create a config class and then every class that needs the values have them extend the config class. Again that seems redundant. </p> <p>Not sure if I'm making much sense I just want an easy way to carry config values throughout every class and use them without having to type overly redundant code.</p> <p>Thanks in advance!</p>
    singulars
    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.
 

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