How to save variable parameters in php?

I am currently writing a program in php. I want the user of my script to change some parameters, such as the location of the database and the user. These variables are placed in the first lines of the script.

Now I want to access these variables from outside the script, as in classes and functions.

I used global variables for this. But a colleague told me not to use globals.

So what is the general way to store and access settings in php?

+4
source share
5 answers

You can use constants that are global by default:

define('APPLICATION_SETTING', true); 

Otherwise, you can use the Singleton Registry class, in which you load the settings and save them in a private class variable. Classic registry class structure:

 class Registry { private $instance = NULL; private $vars = array(); private __construct() {/**/} public static function getInstance() {/**/} public function get($key) {/**/} public function set($key, $value) {/**/} } 

And no, I suggest you not to use injections and patterns. Just use Singleton. With Injection, you get worse and harder.

+5
source

Configuring a script by changing variables inside a script is a bad idea.

The easiest way to save configuration settings is to use the .ini file and parse it using the parse_ini_file function. http://php.net/manual/en/function.parse-ini-file.php

sample ini:

 [db] host = "foo.org" user = "dbguy" [session] timeout = 600 

If your script object is oriented, you should wrap the configuration setting in the class ... and pass it as a constructor argument to your main class.

Otherwise, you can use the global array, but please do not use the 'global' keyword in your functions! Your functions should accept settings from your functions as parameters.

 $config = parse_ini_file("protectedFolder/config.ini", true); //wrong, do not do it this way function connect(){ global $config; $connection = dostuff($config['db']['user'],$config['db']['host']; //do stuff } connect(); //right function connect2($user, $host){ $connection = dostuff($user, $host); //do stuff } connect2($config['db']['user'], $config['db']['host']); 

This is a very simple concept, and there are more efficient ways to do this, but you need to get started, and it will work for simple applications.

If you need something more complicated, you should google for "dependency injection"

EDIT: edited comments EDIT2: fixed missing quotes

+2
source

There are several methods. Here are a few options:

  • You can use a constant if the variable does not change.
  • You can use a single-line configuration object (although single models are generally dissatisfied with many, I personally think that in the case of a global configuration they can be very useful).
  • Alternatively, you can simply pass variables as arguments to a function.

It really depends on the application you are developing and personal preferences. However, global variables tend to be uncomfortable in the long run.

+1
source

You may have a simple sqlite db on the server, perhaps for setting configuration parameters that can only be achieved by your php program. Encapsulating a sqlite query in one simple class, you do not need to use global variables.

0
source

In this case, you, a colleague, are mistaken. Globals are the most convenient way to store settings that should be available everywhere. It is global, that is accessible all over the world. PHP is already filled with globals, for example $_REQUEST . Put your settings in one global array called $SETTINGS , my advice.

-1
source

Source: https://habr.com/ru/post/1384673/


All Articles