Analyze several methods of using global variables in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:04:16
Original
843 people have browsed it

Introduction
Even if you develop a new large-scale PHP program, you will inevitably use global data, because some data requires the use of your code of different parts. Some common global data include: program setting classes, database connection classes, user information, etc. There are many ways to make this data global data, the most commonly used of which is to use the "global" keyword declaration, which we will explain in detail later in the article.
The only disadvantage of using the "global" keyword to declare global data is that it is actually a very poor way of programming, and often leads to bigger problems in the program later, because global data puts you in the code The original separate code segments are all linked together. The consequence is that if you change one part of the code, it may cause other parts to go wrong. So if there are many global variables in your code, then your entire program will be difficult to maintain.

This article will show how to prevent this global variable problem through different techniques or design patterns. Of course, first let's see how to use the "global" keyword for global data and how it works.

Use global variables and the "global" keyword
PHP defines some "Superglobals" variables by default, which are automatically globalized and can be used anywhere in the program. Called in places, such as $_GET and $_REQUEST, etc. They usually come from data or other external data, and using these variables usually does not cause problems because they are basically not writable.

But you can use your own global variables. Using the keyword "global" you can import global data into the local scope of a function. If you don't understand "variable usage scope", please refer to the relevant instructions in the PHP manual.
The following is a demonstration example using the "global" keyword:

Copy the code The code is as follows:

$my_var = 'Hello World';
test_global();
function test_global() {
// Now in local scope
// the $ my_var variable doesn't exist
// Produces error: "Undefined variable: my_var"
echo $my_var;
// Now let's important the variable
global $my_var;
// Works :
echo $my_var;
}
?>

As you can see in the above example, the "global" keyword is used to import global variables of. It looks like it works well and is simple, so why do we worry about using the "global" keyword to define global data?
Here are three good reasons:

1. Code reuse is almost impossible.
If a function depends on global variables, it is almost impossible to use this function in different environments. Another problem is that you can't extract this function and use it in other code.

2. Debugging and solving problems is very difficult.
Tracing a global variable is much more difficult than tracking a non-global variable. A global variable may be redefined in some obscure include file, and even if you have a very good program editor (or IDE) to help you, it may take you several hours to discover the problem.

3. It will be very difficult to understand these codes.
It is difficult for you to figure out where a global variable comes from and what it is used for. During the development process, you may know every global variable, but after about a year, you may forget at least some of them. At this time, you will regret that you used so many global variables.
So if we don’t use global variables, what should we use? Let’s look at some solutions below.
Using Function Parameters
One way to stop using global variables is to simply pass the variable as a function parameter, like this:

Copy code The code is as follows:

$var = 'Hello World';
test ($var);
function test($var) {
echo $var;
}
?>

If you only need to pass a global variable, then this is a very good or even outstanding solution, but what if you want to pass many values?
For example, if we want to use a database class, a program settings class and a user class. In our code, these three classes are used in all components, so they must be passed to every component. If we use the function parameter method, we have to do this:
Copy the code The code is as follows:

$db = new DBConnection;
$settings = new Settings_XML;
$user = new User;
test($db, $settings, $user);
function test(&$db , &$settings, &$user) {
// Do something
}
?>

Obviously, it’s not worth it, and once we have new Objects need to be added, and we have to add one more function parameter to each function. So we need to use another way to solve it.

Use Singletons One way to solve the problem of function parameters is to use Singletons to replace function parameters. Singletons are a special class of objects that can only be instantiated once and contain a static method that returns the object's interface. The following example demonstrates how builds a simple singleton:
Copy the code The code is as follows:

// Get instance of DBConnection
$db =& DBConnection::getInstance();
// Set user property on object
$db->user = 'sa ';
// Set second variable (which points to the same instance)
$second =& DBConnection::getInstance();
// Should print 'sa'
echo $second-> ;user;
Class DBConnection {
var $user;
function &getInstance() {
static $me;
if (is_object($me) == true) {
return $mes; () {
                                       // TODO
    }
}
?>


The most important part of the above example is the function getInstance(). This function ensures that there is only one instance of the DBConnection class by using a static variable $me to return an instance of this class.
The advantage of using a singleton is that we do not need to pass an object explicitly, but simply use the getInstance() method to obtain the object, as follows:



Copy the code
The code is as follows:


function test() { $db = DBConnection::getInstance(); // Do something with the object}?>

However, there are also a series of shortcomings in using singletons. First of all, what if we need to globalize multiple objects in a class? Since we are using a singleton, this is not possible (as its name is singleton). Another problem is that singletons cannot be tested using individual tests, and this is completely impossible unless you introduce the entire stack, which is obviously something you don't want to see. This is the main reason why singletons are not our ideal solution.


Registration Pattern

The best way to make some objects available to all components in our code (Translator's Note: global objects or data) is to use a central Container object, use it to contain all our objects. Usually this container object is called a registrar. It's very flexible and very simple. A simple registrar object looks like this:


Copy the code
The code is as follows:

Class Registry { var $_objects = array(); function set($name, &$object) { $this->_objects[$name] =& $object; } function &get($name) {
return $this->_objects[$name];
}
}
?>


The first step to use a register object is to use the set() method to register an object:
Copy the code The code is as follows:

$db = new DBConnection;
$settings = new Settings_XML;
$user = new User;
// Register objects
$registry =& new Registry;
$registry->set ('db', $db);
$registry->set ('settings', $settings);
$registry->set ('user ', $user);
?>

Now that our register object holds all our objects, we need to pass this register object to a function (instead of passing it separately three objects). Look at the example below:
Copy the code The code is as follows:

function test(&$registry ) {
$db =& $registry->get('db');
$settings =& $registry->get('settings');
$user =& $registry- >get('user');
// Do something with the objects
}
?>

Registrar Compared with other methods, its A big improvement is that when we need to add a new object to our code, we no longer need to change everything (Translator's Note: refers to all the code in the program that uses global objects), we only need to A new object is registered in the register, and then it (Translator's Note: the newly registered object) can be called in all components immediately.

In order to make it easier to use the register, we changed its call to singleton mode (Translator's Note: The function transfer mentioned above is not used). Since we only need to use one register in our program, the singleton pattern is very suitable for this task. Add a new method in the register class, as follows:
Copy the code The code is as follows:

function &getInstance() {
static $me;
if (is_object($me) == true) {
return $me;
}
$me = new Registry;
Return $me;
}
?>

This way it can be used as a single piece, such as:
Copy code The code is as follows:

$db = new DBConnection;
$settings = new Settings_XML;
$user = new User;
// Register objects
$registry =& Registry::getInstance();
$registry->set ('db', $db);
$registry->set ('settings', $settings);
$registry->set ('user', $user);
function test() {
$registry =& Registry::getInstance();
$db =& $registry->get('db');
$settings =& $registry->get('settings');
$user =& $registry->get ('user');
// Do something with the objects
}
?>

As you can see, we don’t need to pass anything private To a function, there is no need to use the "global" keyword. So the registrar pattern is an ideal solution to this problem, and it's very flexible.

Request wrappers
Although our registrar has made the "global" keyword completely redundant, there is still one type of global variable in our code: super global Variables, such as variables $_POST, $_GET. Although these variables are pretty standard and should work fine for you, in some cases you may need to encapsulate them using a register as well.
A simple solution is to write a class to provide an interface for obtaining these variables. This is often called a "request wrapper", here is a simple example:
Copy the code The code is as follows:

Class Request {
var $_request = array();
function Request() {
// Get request variables
$this->_request = $_REQUEST;
}
function get($name) {
return $this->_request[$name];
}
}
?>

The above example is a simple demonstration. Of course, you can do many other things in the request wrapper (such as automatically filtering data, providing default values, etc.).
The following code demonstrates how to call a request wrapper:
Copy the code The code is as follows:

$request = new Request;
// Register object
$registry =& Registry::getInstance();
$registry->set ('request', &$request);
test();
function test() {
$registry =& Registry::getInstance();
$request =& $registry->get ('request');
// Print the 'name' querystring, normally it'd be $_GET['name']
echo htmlentities($request->get('name'));
}
?> ;

As you can see, now we no longer rely on any global variables, and we have completely moved these functions away from global variables.

Conclusion
In this article, we demonstrated how to fundamentally remove global variables from your code and use appropriate functions and variables accordingly to replace. The registration pattern is one of my favorite design patterns because it is very flexible and it prevents your code from becoming a mess.
Also, I recommend using function parameters instead of the singleton pattern to pass the registrar object. Although using singletons is easier, it may cause some problems later, and passing function parameters is also easier to understand.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327793.htmlTechArticleIntroduction Even if you develop a new large-scale PHP program, you will inevitably use global data, because some data Different parts of your code are required. Some common big picture...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!