[wp-hackers] Globalize everything

wordpress at santosj.name wordpress at santosj.name
Fri Dec 7 01:44:16 GMT 2007

Why don't you just use the Registry Pattern[1]? Which is what you did, but
I would hate you forever if I seen that code in production.

Usually, globals introduced in code have the possibility of clashing with
other globals in the same or another environment. Granted, it is unlikely
someone will name their global variable $wp_filters (if they enjoyed
living), but you'll never know.

With an Registry pattern a fool would have to go out of their way to mess
with your code and so you can assume it was on purpose (then the hate
could be brought down upon the poor soul).

I think what your code does is a more hackish attempt at the Registry

A couple of problems with your code.

1. $g is a copy, therefore unsetting it will have no effect whatsoever on
the original $GLOBALS array. <?php unset($GLOBALS[$g]); ?>

2. Anything that uses variable variables needs to be rewritten to not be
variable variables (in production code).

3. The performance hit that would be your code would be massive, depending
on two factors. A) How many globals there are and B) how many functions
actually use your method. Not to mention eval() is slow.

4. You would in effect make all globals available to your function and
have an even greater chance of clashes, negating the entire advantage of
explicitly declaring which globals you want available in function local

5. I'm not exactly sure you can use variable variables with super globals
(*wonders if $GLOBALS is actually a super global*). However did you test
your code? If so, then I learned something new today.

[1] http://trac.wordpress.org/ticket/5384 - See my patch

Jacob Santos

> Don't you hate it when you forget to "global $wpdb;"? Here is a solution:
> <?php
> $a = 'Andy';
> function g() {
> 	return 'foreach(array_keys($GLOBALS) as $g)global $$g;unset($g);';
> }
> function go() {
> 	eval(g());
> 	var_dump($a);
> }
> go();
> ?>
> Andy
> (Sorry, couldn't wait for April.)
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list