Teaches you how to use global variables in PHP. Even if you develop a new large PHP program, you must use global data, because some data requires different parts of your code. Some common global data, even if you develop a new large PHP program, you will inevitably use global data, because some data requires different parts of your code. Some common global data includes program setting, database connection, and user data. There are many ways to make this data a global data. The most common one is to use the "global" keyword Declaration. we will explain it in detail later.
The only drawback of using the "global" keyword to declare global data is that it is actually a very poor programming method, and it often causes a larger problem in the program, because global data links all the original code segments in your code, the consequence is that if you change a part of the code, other parts may encounter errors. Therefore, if your code contains many global variables, your entire program will inevitably be difficult to maintain.
This article will show how to prevent such global variables through different technologies or design patterns. Of course, let's first look at 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. these variables are automatically normalized and can be called anywhere in the program, such as $ _ GET and $ _ REQUEST. They generally come from data or other external data. using these variables will not cause problems, because they are basically not writable.
However, you can use your own global variables. With the keyword "global", you can import global data to a local range of a function. If you do not understand the scope of use of variables, please refer to the relevant instructions in the PHP Manual.
The following is an example of using the "global" keyword:
Reference content is as follows:
<?php $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 preceding example, the "global" keyword is used to import global variables. It seems to work very well and very easy. why do we need to worry about using the "global" keyword to define global data?
The following 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 cannot extract this function and use it in other code.
2. it is very difficult to debug and solve the problem.
It is more difficult to trace a global variable than to trace a non-global variable. A global variable may be redefined in some non-obvious include files, even if you have a very good program editor (or IDE) to help you, it takes you several hours to discover the problem.
3. Understanding the code will be very difficult.
It is hard to figure out where a global variable comes from and what it is used. During development, you may know every global variable, but after about a year, you may forget at least general global variables, at this time, you will regret using so many global variables.
So what should we use if we do not use global variables? Next let's look at some solutions.
Use function parameters
One way to stop using global variables is to simply pass variables as function parameters, as shown below:
Reference content is as follows:
<?php $var = 'Hello World'; test ($var); function test($var) { echo $var; } ?> |
If you only need to pass a global variable, this is a very good or even an outstanding solution. but if you want to pass many values, what should you do?
For example, if we want to use a database class, a program setting class, and a user class. In our code, these three classes are used in all components, so they must be passed to each component. If we use the function parameter method, we have to do this:
Reference content is as follows:
<?php $db = new DBConnection; $settings = new Settings_XML; $user = new User; test($db, $settings, $user); function test(&$db, &$settings, &$user) { // Do something } ?> |
Obviously, this is not worth it. once we have a new object to be added, we have to add one more function parameter for each function. Therefore, we need to use another solution.
Bytes. Some common global data...