Latest Uploads
Still Truckin'

rychan

Curlicue Fractals

Andy_A

heroextant ... -small.png

HoboBen

image-25-0 ... 26-04.jpeg

Jayenkai

Help_Fight ... rivacy.png

Jayenkai

Gameboy...Colour!!

rychan

Forum Home

vs PHP

UserMessage
Posted : Friday, 03 February 2012, 19:35 | Permalink
JL235


WW Entries : 7
due to the way PHP automatically makes top level variables globally accessible, it means the moment you import a script within a function, the script breaks with strange errors. I’m currently battling this in Wordpress!

-----
PlayMyCode.com - build and play in your browser, Blog, Twitter.
Homepage : http://www.StudioFortress.com
Posted : Friday, 03 February 2012, 19:35 | Permalink | Mark Here
Stealth


I don't think PHP is to blame as much as the programmers. You could do what you describe in Javascript, but it's not the languages fault. The barrier to entry in PHP is so low that it breeds bad code. I'm not saying PHP is a great language, but it can be used in ways that produce decent code.

-----
Andrew // stealth
"Some people see things as they are and say why? I dream things that never were and say why not?" - Robert Kennedy
Homepage : http://strubhar.me
Posted : Saturday, 04 February 2012, 01:21 | Permalink | Mark Here
JL235


WW Entries : 7
Actually no, if you did the same in JavaScript, it would actually work.

-----
PlayMyCode.com - build and play in your browser, Blog, Twitter.
Homepage : http://www.StudioFortress.com
Posted : Saturday, 04 February 2012, 02:49 | Permalink | Mark Here
JL235


WW Entries : 7
I thought I'd add a bit more clarification on the problem. I've also simplified the code a little to help remove any distractions.

The problem is that in PHP, global variables are ones explicitely stated to be global, or are defined outside any functions (at the upper most level of scope). It's that second rule which is the gotcha.

Wordpress defines some common globals which are used through out it, for example:

-->

Here PHP automatically makes the outer $wp variable global, since it is outside a function, and so the wordpress functions can all access it.

However this breaks if you ever want to load Wordpress dynamically. This is because you'll often end up doing this from inside a function. For example:

-->

... which ends up becomming ...

-->

Now $wp is defined inside a function, and so it's no longer automatically global. As a result, the 'wordpressFun' fails to alias it. As a result, there are now two $wp variables. The local one, which contains the 'WP' object, and the global one which contains null.

If you did the same in JavaScript, it would work:
-->

Since functions can always alias variables outside them, regardless of if that is done at the top level scope or not.

If you remove the 'var', then JavaScript will also automatically make it a global variable, at any level of scope! PHP only does this at the top level.

The problem here is that PHP's approach is inconsistent. It does one thing when run at one level of scope, and something entirely different at any other. This is especially annoying since it's normal for scripts to get included within functions.

-----
PlayMyCode.com - build and play in your browser, Blog, Twitter.
Homepage : http://www.StudioFortress.com
Posted : Saturday, 04 February 2012, 07:18 | Permalink | Mark Here
HoboBen


WW Entries : 9
I don't know if you want a work-around or just to moan at PHP (which is a totally normal thing to do!) but one idea would be a global associative array.

The Wordpress file:

-->

Obviously that could be annoying to do to every script.


-----
Posted : Saturday, 04 February 2012, 07:24 | Permalink | Mark Here
HoboBen


WW Entries : 9
I found this in the php.net user-supplied comments

-->

Might be useful. Also of note is using "extract" function to dump an associative array directly into the local symbol table, which simplifies what I suggested (although you probably won't want ALL of your globals dumped in that way).

-----
Posted : Saturday, 04 February 2012, 10:42 | Permalink | Mark Here
JL235


WW Entries : 7
You can just add:
-->
.. just before you include. So then your essentially adding the global declarations missing from Wordpress. Although it took about 3 to 4 hours of debugging for me to work that out!

But my rant above was more about the gotcha it's self, how variables are presumed to be global in one place and not in another. Especially to point out that it's not the same behaviour you get in other languages.

-----
PlayMyCode.com - build and play in your browser, Blog, Twitter.
Homepage : http://www.StudioFortress.com
Posted : Saturday, 04 February 2012, 10:50 | Permalink | Mark Here
HoboBen


WW Entries : 9
The only thing about adding the globals before you include is obviously you need to know what is in the script before you include it (breaking encapsulation).

Unless reintroducing everything into global scope is OK, in which case, there's no problem.

-----
Posted : Saturday, 04 February 2012, 11:29 | Permalink | Mark Here
JL235


WW Entries : 7
Agreed.

However when you include Wordpress, it'll run off and touch these global variables straight away inside of functions, and then crash (because of the global rant above). The bug occurres in Wordpress code. So you must resolve the issue before you include Wordpress core.

Pre-stating them as global does this without needing to edit Wordpress core, and it'll probably survive future updates.

-----
PlayMyCode.com - build and play in your browser, Blog, Twitter.
Homepage : http://www.StudioFortress.com
Posted : Saturday, 04 February 2012, 16:31 | Permalink | Mark Here
Stealth


The Wordpress developers should have realized this and wrote better code. PHP does some weird things but it should never be the blame for bad code. That's the developers fault.

-----
Andrew // stealth
"Some people see things as they are and say why? I dream things that never were and say why not?" - Robert Kennedy
Homepage : http://strubhar.me
Posted : Friday, 17 February 2012, 19:01 | Permalink | Mark Here
JL235


WW Entries : 7
I've been hit a second time by this!

On Play My Code we have that the Google bot tries to setup games for playing, and the JS crashes. So we just disable the setup code for web crawlers.

After the last release, this has mysteriously stopped working. Turns out the problem is because I moved the script loading into it's own function, to localize variables, so my internal variables don't clash with those in scripts.

One of those scripts uses a global variable, but it's not declared. So it breaks, with no warning!

-----
PlayMyCode.com - build and play in your browser, Blog, Twitter.
Homepage : http://www.StudioFortress.com
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 634|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Latest Posts
PIP Crap
Dabz Thu 08:56
What Size is the Image
Jayenkai Thu 02:59
Offsetting Time
Jayenkai Thu 00:30
AGameAWeek : 2016 - Part One
rychan Wed 13:10
Mystery Milk
steve_ancell Wed 04:46
RIP : Burt Kwouk
Jayenkai Tue 10:32
Vehicle For Sale
Jayenkai Tue 05:31
Unexplainable Compiler Slowdown
therevillsgames Mon 15:32
Sausage Party
Jayenkai Mon 05:09
Gameboy Racing Game in progress.
rychan Sun 16:03
More

Latest Items
Snippet : Blurring and Dripping, rectangles
Pakz Thu 05:52
Showcase : Curlicue Fractals
Jayenkai Sun 08:41
Showcase : Grey backgrounds
Pakz Sun 07:43
News : Newsletter #265
steve_ancell Fri 02:58
Showcase : Jumble Blocks
steve_ancell Mon 09:44
Showcase : Harpoons and Balls
Ninjinister Mon 07:29
Showcase : You Shall Die
rychan Sun 12:47
Showcase : Mojo2 html5 animation
Pakz Wed 14:05
Snippet : Little Spinning Grid
Pakz Wed 13:45
Snippet : Make Trees
HoboBen Tue 16:11
Showcase : No Plumbing Required
steve_ancell Mon 10:51
Blog : Feedback
Pakz Fri 14:05
Showcase : 8-Bit Boulders
steve_ancell Wed 08:19
Dev-Diary : PS2 to N64 Adapter
Jayenkai Wed 03:07
Dev-Ideas : Pixel editor
Jayenkai Mon 23:40
More

Who's Online
Pakz
Thu, at 10:17
HoboBen
Thu, at 10:11
GfK
Thu, at 10:04
Jayenkai
Thu, at 10:04
Dabz
Thu, at 08:56
spinal
Thu, at 08:56
steve_ancell
Thu, at 06:05
rockford
Thu, at 03:53
9572AD
Wed, at 22:28
therevillsgames
Wed, at 19:02
Link to this page
Site : Jayenkai 2006-Infinity | MudChat's origins, BBCode's former life, Image Scaler.