debuggable

 
Contact Us
 

Need to share global variables throughout your app?

Posted on 11/6/06 by Felix Geisendörfer

Deprecated post

The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.

Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.

Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).

I just came into a situation where I needed to share a global variable throughout my cakephp app, and I think I found a great way to accomplish it. CakePHP has a cool little class called Configure that is used to store path's to controllers, models and views. The interesting thing is, that you can use it for you personal needs as well. Simply do this somewhere in your app (even works in bootstrap.php):

$config =& Configure::getInstance();
$config->myVariable = 'Hello World';

Now you can access myVariable anywaywhere inside your application by doing:

$config =& Configure::getInstance();
debug($config->myVariable);

I like this method way better then the php 'global' keyword which seems pretty messy to me. I'm also a big fan of this Class::getInstance() pattern that is used throughout cakephp - really neat!

Anyway, don't make more use of this then neccessary, because CakePHP has a really good (mvc) convention about what should be accessible from where and it's a good idea to stick with it.

--Felix Geisendörfer aka the_undefined

 
&nsbp;

You can skip to the end and add a comment.

scot lewis said on Jun 12, 2006:

That would be the Singleton pattern. There's a growing school of thought that it is a n artifact of over-OOPing, and should really be redressed as a collection of static methods. (Inside the static methods, you can still use the getInstance call, but then it's an implementation detail the user needn't consider.)

Obligatory Wikipedia Link: http://en.wikipedia.org/wiki/Singleton_pattern
The case for static methods: http://opal.cabochon.com/~stevey/blog-rants/singleton-stupid.html

The c2/Portland Pattern Repostory's Singleton Page: http://c2.com/cgi/wiki?SingletonPattern

The c2/PPR page deprecating the Singleton Pattern: http://c2.com/cgi/wiki?SingletonPatternDeprecated

There's a lot more in the PPR (of course), it's well worth clicking around. (After all, it was the first wiki and proved the concept..)

[ Links as text because I don't know how your commenting system handles HTML/BBCode/etc and I don't feel like running through the multiple comment dance to find out. :) ]

scot lewis said on Jun 12, 2006:

Hmm... amend that first paragraph to read:

That would be the Singleton pattern. There’s a growing school of thought that it is an artifact of over-OOPing, and should really be redressed as a collection of static methods, or Factory, or another, much more direct modeling of the problem at hand. (Inside the static methods, for example, you can still use the getInstance call if necessary, but then it’s an implementation detail the user needn’t consider and everybody gets cleaner code.)

Once day I'll learn to step away for 5 minutes before hitting submit...

Felix Geisendörfer said on Jun 12, 2006:

hey, scot lewis: thanks for that comment. Yeah my comment filters are pretty strict since this blog started to get a lot of spam. I use akismet, as well as a blacklist together with a rule that says more then 2 links in a comment cause it to go to moderation. I wish there weren't as many spam bots, so I could use a less restricting setup ...

I figured it was some kind of pattern, I just didn't bother to look it up for now. So what you are saying is that having a static method would be cleaner? Hmm, I do agree that calling Class:getInstance() inside the functions of your class direcly is a good idea, but in this case we need to access a value so it's the only way.

How would you share a global value throughout the app? I needed an array with all languages supported, as well as the active language, which was why I used this class.

--Felix aka the_undefined

[...] Need to share global variables throughout your app?: "I just came into a situation where I needed to share a global variable throughout my cakephp app, and I think I found a great way to accomplish it. CakePHP has a cool little class called Configure that is used to store path's to controllers, models and views. The interesting thing is, that you can use it for you personal needs as well. Simply do this somewhere in your app (even works in bootstrap.php): PLAIN TEXT PHP: [...]

ucakkurzxg said on Jan 12, 2007:

Hello guys! I have some questions. I mean need some help.
Where i can read more about this problema?

Please, don't derect me to http://google.com i know about it.

Please derect me with some links.

thanks!

UCAKK^^

Felix Geisendörfer said on Jan 12, 2007:

ucakkurzxg: Please identify yourself as a human being and explain what kind of information you are looking for exactly. Otherwise your post has this bad 'spam-bot-feeling' about it that could make me delete it.

kraek  said on Mar 07, 2007:

I have the problem that i wish to exchange veriables over several controllers.. and while in java this would be easy to fix, i cant seem to solve it in cakePHP.

I tried your method, and while in the controller i added your variable it is present... there is no trace of it anymore in another controller.

This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.