-1

I want to initialise a variable which i need to use across my zend application. Where am i supposed to initialise it ? In index.php or in bootstrap.php ?

FrancisMV123
  • 3,419
  • 4
  • 20
  • 20

4 Answers4

3

Bootstrap is the correct place. Bootstrap is used for web applications and CLI applications against index.php is only used when the app is used as a web app.

Maks3w
  • 6,014
  • 6
  • 37
  • 42
  • +1 for not mentioning the registry. The registry is a cheap trick around the way Zend works. – nwalke Sep 13 '12 at 15:05
  • @tubaguy50035 why so much hate for the Zend registry? :) – codisfy Sep 13 '12 at 15:07
  • As @Maks3w points out, the "correct" way is the Bootstrap. You see all these sites that pass around things like Doctrine's EM in the registry, and it's icky. Doctrine is a "resource" that should be loaded by the bootstrap and then you have access to the EM by returning it in your resource. It just doesn't make me feel good using it lol – nwalke Sep 13 '12 at 15:09
  • @Maks3w is it really so? What I thought was ZF2 has only the core components and more components can be included if required. If it really has been removed and deprecated, could you plz find me a resource mentioning this? – codisfy Sep 14 '12 at 04:20
  • @Maks3w forget to mention a thanks. Don't get it in any other way :) – codisfy Sep 14 '12 at 04:37
  • @codeHeart ZF2 includes a layer of service manager and dependency injection so any thing that the script needs is injected. Also Zend_Registry is a problem when you use unit tests – Maks3w Sep 14 '12 at 05:23
0

For storing values globally.You can use the zend registry. Like

Zend_Registry::set('index', 'value');

and retrieve it anywhere by :-

Zend_Registry::get('index');

Quoting the Zend manual :

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

codisfy
  • 2,155
  • 2
  • 22
  • 32
0

I'd say it really depends on the kind of variable that you want to use. If you want to define something like the system path or the public url that could be use anywhere in the application, you could use a define('varname', 'value') in your index.php file. I'd really keep only some major things in there that are only used by the core system else your code will quickly become a mess. For the rest, it could be initialized in the Boostrap.php file.

Joel Lord
  • 2,175
  • 1
  • 18
  • 25
-1

You can put it in the registry (i.e Zend_Registry) on your boostrap or put it in your config file (i.e application.ini).

conradfr
  • 184
  • 2
  • 8