Is there a way to change the include in the function to global incdue? because after the function is included in the file, all the variables in the file are changed to local variables in the function, although the global declaration can be made in the function in advance, but it is troublesome for a large number of variables, and these variables must be converted to global variables for use elsewhere through $ GLOBALS. Is there a way to dynamically load global variables using the script tag like JavaScript ?!
For example:
A. php
Finally:
A. php
But I think it is impossible, because although php is a sequential execution program like js, js can rely on the HTMLDom mechanism to dynamically load scripts, but php does not have the environment, unless php re-constructs the include mechanism, all the variables after the include file function are automatically added to the global variable $ GLOBALS by referencing or copying.
Right ?!
Reply to discussion (solution)1. do not compare php with js. the operating mechanisms of the two are different.
2. you say that you use the script label to dynamically load global variables like js, but this script label represents a global program segment. Not in the function
3. although your requirements are strange, they can also be implemented. Of course, we still need to use the $ GLOBALS channel.
function fn($_Name) { include "{$_Name}.php"; foreach(get_defined_vars() as $_K=>$_V) { if(! in_array($_K, array('_Name', '_K', '_V')) $GLOBALS[$_K] = $_V; }}
This post was last edited by xuzuning at 08:46:41
1. do not compare php with js. the operating mechanisms of the two are different.
2. you say that you use the script label to dynamically load global variables like js, but this script label represents a global program segment. Not in the function
3. although your requirements are strange, they can also be implemented. Of course, we still need to use $ GLOBA ......
It is still a bit inappropriate because get_defined_vars () will automatically detect the object (which cannot be determined using is_object () and try to convert it into a string. if _ toString () is not defined in the class () will throw a fatal error. In addition, if the loaded file has treated the previous variables as global variables, it makes no sense to convert them later.
This post was last edited by xuzuning at 08:46:41
1. do not compare php with js. the operating mechanisms of the two are different.
2. you say that you use the script label to dynamically load global variables like js, but this script label represents a global program segment. Not in the function
3. although your requirements are strange, they can also be implemented. Of course, we still need to use $ GLOBA ......
I do have this requirement, but to avoid weaknesses, the function only needs to determine whether the file exists. If yes, it should be handed over to the previously defined global variables and then include them outside the function, rather than in the function.
I am here to learn