Home > Backend Development > PHP Tutorial > php7 - PHP global variables use composer to automatically load and are not available

php7 - PHP global variables use composer to automatically load and are not available

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-06 13:53:49
Original
1178 people have browsed it

This is historical legacy code. The original programmer directly instantiated the database instance in the configuration file

$db = new db();

If this kind of code is normally included using require/include, there will be no problem. When it is judged, $GLOBALS['db'] is used to judge.

Because there are a lot of files, I want to reuse some of the models. Because of the inclusion relationship, I have to try them one by one and then include them. I want to make them load automatically, so I used composer to automatically load the files. $GLOBALS['db'] is empty. I usually don't use this global parameter, so I don't know what the situation is.

I have solved this problem myself, just change $db = new db() to $GLOBALS['db'], that is, assign the value explicitly.

I don’t understand why $db that is included manually will exist, but $db that is automatically loaded using composer will disappear. Please give me some guidance.

Reply content:

This is historical legacy code. The original programmer directly instantiated the database instance in the configuration file

$db = new db();

If this kind of code is normally included using require/include, there will be no problem. When it is judged, $GLOBALS['db'] is used to judge.

Because there are a lot of files, I want to reuse some of the models. Because of the inclusion relationship, I have to try them one by one and then include them. I want to make them load automatically, so I used composer to automatically load the files. $GLOBALS['db'] is empty. I usually don't use this global parameter, so I don't know what the situation is.

I have solved this problem myself, just change $db = new db() to $GLOBALS['db'], that is, assign the value explicitly.

I don’t understand why $db that is included manually will exist, but $db that is automatically loaded using composer will disappear. Please give me some guidance.

Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template