Marcel
Marcel

Reputation: 6300

Does Zend Framework turns off declaring variables as global?

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?

Upvotes: 1

Views: 2395

Answers (2)

Alana Storm
Alana Storm

Reputation: 166066

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();
}

Upvotes: 8

hobodave
hobodave

Reputation: 29303

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.

Upvotes: 1

Related Questions