📜 ⬆️ ⬇️

Load function for own framework

Hello dear PHP experts. Today I want to ask you advice how to be.

About the need to write my own “bicycle”, a framework, I would not want to go into it, it was just necessary. Maybe I'm not doing everything right, correct if so, my extensions are loaded via the load ($ path) function in which you can insert then the definition of an absolute path or a ban on loading something, in general, it is not just require_once ( ). However, I ran into a big problem - variables ...

 function load($path) { static $debug; if (file_exists($path)) { $debug[] = "load($path): Success"; require_once( $path ); return true; } else { $debug[] = "load($path): Path not exists"; return false; } } 

')
I ran into a problem when I started loading modules through it. The fact is that all variables of the loadable module fall into the function load () and safely die in it without falling into global variables. That is, I cannot assign a variable in one module, and use it in another. Now the question is how to get around it?

One way: in the load () function, add global $ html, $ ajax, $ user, $ tags, $ info ... here I list all the variables that I need to “transfer” from module to module. We have to replenish the list of these variables in the course of work on the project - it is not convenient. Another way: declare global one global variable $ _G; and use it only for data transfer between modules - it is also not good, it imposes certain restrictions in the project. Transfer the entire $ GLOBALS to a function via foreach () {global $ ($ var); } too wasteful ... and the enumeration of get_defined_vars () is necessary in theory before the module is loaded.

Please advise how to be?

Source: https://habr.com/ru/post/40446/


All Articles