[wp-hackers] register_activation_hook() problem

Jeremy Visser jeremy at visser.name
Wed Jul 29 00:56:51 UTC 2009


On Tue, 2009-07-28 at 11:21 -0500, Chris Jean wrote:
> Scope is really odd in PHP; frankly, I don't like how it works. So,
> forget everything you knew about scope in other languages and learn how
> PHP does it.
> 
> When you create a function, you create a new scope completely-unique to
> that function. Nothing exists to that function except what is passed to
> it as an argument or what is created inside of it. Even though a
> variable may be global, it does not exist inside any function.
> 
> The way you bring a global into the function is to use the global
> keyword to bring the global variable into the function's scope.

Not that this is of much relevance, but I've been coding in Python
lately, and it has the same characteristics, but with an additional
twist:

Global variables may be accessed from within a function without being
declared as such, but as soon as you try to assign a value to the
variable, it instantly becomes local scope, and doesn't affect the
global version. So you need to declare with with a "global myvar"
statement.

And it gets even hairier. Only assignment to global variables (e.g. "x =
25") is affected. If you have a list (basically synonymous with an array
in PHP, except only numeric keys are accepted), e.g. my_list = ['tom',
'dick', 'harry'], you can use the append() function of the list, e.g.
my_list.append('john') without declaring global, but then you try to
assign a new list, e.g. my_list = ['judith', 'bertha', 'gladys'], it
will then become local scope and will create weird errors.

That said, Python is still a great language. But I don't like Django. :(



More information about the wp-hackers mailing list