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
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-06 13:53:491185browse

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.

Statement:
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