On Sat, Jun 14, 2008 at 07:14, Larry Garfield <larry@garfieldtech.com> wrote:
On Friday 13 June 2008, Bob Pepin wrote:
btw, drupal isn't perfect either, just earlier I spent fifteen minutes hunting a bug just because I dared to create a variable called $theme in some toplevel code.. turned out that global was already taken by the drupal theme system, which apparently doesn't bother too much with error checking, so all I got was a blank page, no error message, no nothing.. of course PHP isn't completely innocent in this either ;)
Creating a global variable in the first place was your first mistake. They are a design flaw by definition for exactly the reason you describe. Creating one that was not namespaced to your module was mistake #2, for the reasons you describe. :-)
Well, since PHP does have neither namespaces nor modules (okay, you can use classes as some kind of poor man's namespaces if you don't mind writing ClassName::function() all the time), mistake #2 was unavoidable ;) My point though was that $theme is not exactly the ideal name for a global variable used inside a library. But since the library I'm talking about was previously simply a little module living in its little closed drupal world, I can understand why it turned out the way it did.
You cannot really error check globals in that way, which is part of the problem with them.
Well in this case $theme was an instance of one of my own classes, whereas the library was treating it as a string. Something that in most languages would've either been detected at compile time or would have signaled an error at runtime instantly. And for error checking globals in general, simply requiring variables to be declared before use and signaling an error when a variable with the same name already exists can prevent those conflicts (this would work similar to how PHP prevents you from accidentally redefining a function). And then of course being able to create properly lexically scoped variables (like the "let" keyword that exists in various languages for example) would avoid the problem altogether. Okay, enough off topic for today, have a nice weekend ;) Bob