1
votes

I am integrating third party code into the web application I am writing in Zend Framework.

The code I want to integrate declares variables as global. It works on its own, but not when I include it within Zend Framework. Initially I suspected that there is something in Zend Framework that is disabling the declaration of global variables. I have the following test code in a controller:

public function testglobalAction()
{
   $a = 1;
   function b()
   {
      global $a;
      echo $a*2;
   }

   b();
}

When I ran it prints out 0, as opposed to 2. On top of that running the same code on its own in the same web server prints out 2.

I understand that I could replace all the global instances to use Zend Registry. However, a grep showed me that there are roughly 700 lines I have to change, so it is not feasible at the moment.

Does anyone know how I can solve this problem?

2
Globals are not a good idea in PHP. Zend is doing you a favor. - RaYell
I knew that would be the first comment/answer I'd get with this question! :) But I really integrate this code into my application. - Marcel
Provide the code as you run it from the controller. I don't see anything related to ZF in your sample. Oversimplification = we can't help you. - hobodave
Edited the question with the controller action. It is a really simple code as I am just trying to test if my theory is correct. - Marcel

2 Answers

8
votes

Your original $a variable isn't global.

Any variable declared inside of a method is local to that method, unless it's been previously declared global in the current scope.

Try this

public function testglobalAction()
{
    global $a;
    $a = 1;
    function b()
    {
        global $a;
        echo $a*2;
    }

    b();
}
1
votes

No. Zend Framework doesn't disable globals, as it is not possible. The $GLOBALS array is controlled by the php.ini register_globals directive. It cannot be changed at runtime using ini_set.

See the documentation for reference.

Note: Check your .htaccess files for any per-directory php_value overrides.