PDA

View Full Version : PHP-based menu & possible eval() problem


Linda A
08-03-2006, 09:08 PM
I've got a menu system that uses php to determine which section of my site is currently being viewed, so that the correct menu can be shown. This system has worked fine while the code has been stored in files on my server, but now I am trying to move it into the CMS I am using (ExpressionEngine) and I am running into problems. The code in question can be viewed within these files:

http://www.hippoiathanatoi.com/Files/Menu-Array.txt (this code determines the 'Menu' variable)

http://www.hippoiathanatoi.com/Files/Menu-Main.txt (this is one of the menues using the 'Menu' variable)

http://www.hippoiathanatoi.com/Files/Menu-OnLoad.txt (this code passes the 'Menu' variable to the javascript)

http://www.hippoiathanatoi.com/Files/Menu-Sub.txt (this is one of the menues using the 'Menu' variable)

A member of the support team for ExpressionEngine suggested that this is because the templates (which do allow php within them) use eval() to process php, and that this results in the variables not being available globally. More specifically, he quoted this from their knowledge blog:


PHP used within a template is processed by the PHP function eval(). Because of this, variables in your PHP code are all considered to be of local scope, and your functions will not be able to reference them unless they are explicitly declared as globals.


He did, however, think that the code could be reworked to take this account ... but I am afraid that I have good idea how. I did try simply placing 'global' in front of the $Menu variable, as the example seemed to suggest could be done, but that didn't do the trick. However, I had a lot of help putting this code together in the first place, and my knowledge of php is quite limited. So, if anyone could help me out I'd really appreciate it. :)

Fumigator
08-03-2006, 09:45 PM
That guy told you it was possible to use your variables globally but then didn't tell you how to make that happen? That wasn't very nice of him, that meanie.

If that's all you need to do is make your variables global then you should use the "global" keyword in every function you need access to that variable, not just in the function that defines that variable.

If there are other problems, give us the specifics and we can try to help.

Linda A
08-03-2006, 09:50 PM
Well, they give support on their software, but not user-created php. :)

He thought the problem was just that eval() was being used for parsing in their templating system, but I am not sure if that is the only problem. However, I figured I could try to eliminate potential issues one by one.

The 'Menu' variable is declared like this in the Menu-Array:

$Menu = $WhichMenu[$CurrentSection];

It is the used like this in the other sections:

if ($Menu[0] == "SiteInfo")

In these examples, how would I declare the variable as a global?

NancyJ
08-03-2006, 09:55 PM
put global $Menu;
at the top of each function or code snippet that uses $Menu. If it is a local/global issue, that should fix it.

Though php.net say this Also remember that variables given values under eval() will retain these values in the main script afterwards.

But it cant hurt to try

Linda A
08-03-2006, 10:17 PM
Putting global $Menu; at the start of each piece of php does seem to have solved the issue. Some further testing needed, but that may have been all it took. :)

I don't quite follow what the consequences of the bit you quoted might be, though. Is it indicating potential conflicts, or?

Either way, thank you. :)

NancyJ
08-03-2006, 10:38 PM
Putting global $Menu; at the start of each piece of php does seem to have solved the issue. Some further testing needed, but that may have been all it took. :)

I don't quite follow what the consequences of the bit you quoted might be, though. Is it indicating potential conflicts, or?

Either way, thank you. :)
No just saying that variables set in eval() are available to the main script - though it doesnt say that they will be available to other eval() snippets - but global scope seems to have fixed your problem so I wouldnt worry about it