PHP: "Global" enable

Current situation:

  • I have a current version of my MVC Framework that uses classes as controllers.
  • I have some “vintage” modules from my old MVC Framework, which uses simple, flat versions as controllers.

Significant simplification means:

A new version:

<?PHP class blaController extends baseController { private $intVar; function dosomethingFunction() { $this->intVar = 123; $this->view('myView'); } } ?> 

Old version:

 <?PHP $globalVar = 123; // view "controllername" is automatically shown ?> 

Now I'm trying to write a shell to be able to use my old controllers in my new MVC without rewriting everything. For this, I have a wrapper controller:

 class wrapController extends baseController { function dosomethingFunction() { require 'old_dosomething.function.php'; $this->view('old_dosomething_view'); } } 

(Once again: this is VERY, VERY simplified - just to understand that this is not real code.)

The problem with this approach is that previously the global variable $ globalVar now exists only inside the "dosomethingFunction" method and cannot be accessible for presentation.

This is not so if I could make the requirement behave as "in the global scope", so $ globalVar will be available again in the global scope.

So: is there a way to achieve " require_global " or something like that?

(One solution to my problem was to change my old controllers to start with a bunch of "global" commands, but I would prefer a solution in which I don't need to change such old code.)

(Note: Please don’t tell me that GLOBALS are bad. This is completely out of the question. Just accept that it is a requirement to keep the old code in a newer, cleaner environment.)

+4
source share
5 answers

You can add local variables defined in dosomethingFunction () to the global scope:

 class wrapController extends baseController { function dosomethingFunction() { require 'old_dosomething.function.php'; //begin added code $vararr = get_defined_vars(); foreach($vararr as $varName => $varValue) $GLOBALS[$varName] = $varValue; //end added code $this->view('old_dosomething_view'); } } 

Please note that for this, as you would expect, you must call a query before using any other function. get_defined_vars () returns only variables from the current scope, so no array_diff hacking is required.

+5
source

This is the easiest solution I can think of.

Use the get_defined_vars () function twice and get the difference of each call to determine which variables were entered by the required file.

Example:

 $__defined_vars = get_defined_vars(); require('old_dosomething.function.php'); $__newly_defined_vars = array_diff_assoc($__defined_vars, get_defined_vars()); $GLOBALS = array_merge($GLOBALS, $__newly_defined_vars); $this->view('old_dosomething_view'); 
+4
source

Hmmm, this is a problem that I have never seen before. I suppose you could do it

 class wrapController extends baseController { function dosomethingFunction() { require 'old_dosomething.function.php'; // Force "old" globals into global scope $GLOBALS['globalVar'] = $globalVar; $this->view('old_dosomething_view'); } } 

But this is a rather tedious, manual process, depending on how many globals we speak. I’ll think about it, but I don’t know a single solution to “auto-magic” from the head.

+1
source

For all who are interested: My (so far) final version:

 class wrapController extends baseController { function dosomethingFunction() { // ... do some initialisation stuff ... $__defined_vars = array_keys(get_defined_vars()); require 'old_dosomething.function.php'; $__newly_defined_vars = array_diff( array_keys(get_defined_vars()), $__defined_vars, array('__defined_vars') ); foreach ($__newly_defined_vars as $var) { $GLOBALS[$var] = &$$var; } $this->view('old_dosomething_view'); } } 

Ugly, but it works. Thanks for your great help!

+1
source

Have you tried Zend_Registry from the Zend Framework?

A registry is a container for storing objects and values ​​in an application space. By storing the value in the registry, the same object is always available in your application. This mechanism is an alternative to using global storage.

http://framework.zend.com/manual/en/zend.registry.html

0
source

All Articles