[wp-hackers] register_activation_hook() problem
wordpress at santosj.name
Wed Jul 29 01:15:34 UTC 2009
Yeah, this thread is filled with WTF?s. It is like GOTOs, you can use them, but the language is going to kick your ass for doing so. Well, *you* should never use GOTO and I'm talking to you Adam.
Python is telling you that you shouldn't be using globals. You shouldn't be using globals in PHP. It is a shame really that WordPress uses them so much in the code. With PHP4 support, it is difficult to get around it, but not impossible. I think the Registry Pattern tries to get around it, but I'm unsure whether the additional complexity outweighs the benefit of not using globals.
Globals in and of themselves aren't evil. I guess it is similar to the bad rep that GOTO gets, in that people use them because they are convenient and not for the few instances they legitimately should be used in code.
The reason functions have that scope is because a new stack is created for it. The purpose of a function would be defeated if every variable that exists is passed to the function (would it be the same as using GOTO?, just less WTF?). It is pretty tricky in programming languages, in that one aspect of them is that they require you do a bit of work and understand the fundamentals of programming.
On Wed, 29 Jul 2009 10:56:51 +1000
Jeremy Visser <jeremy at visser.name> wrote:
> 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
> 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"
> 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. :(
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
Jacob Santos <wordpress at santosj.name>
More information about the wp-hackers