php5 ignoring 'define' and 'defined'?

Status
Not open for further replies.
My server provider recently and unexpectedly updated my configuration when updating cPanel and broke my website. I've temporarily worked around this by configuring parsing with php4, but would rather fix the problem and not the symptom.

In my code, I use several libraries and use and the following mechanic to keep an entire file from being included again in subsequent require calls:
Code:
if (defined("_{FILENAME}.PHP")) { return; };
define("_{FILENAME}.PHP", "1");
(For reasons I don't quite remember, require_once was not working when the application was being developed.)


For some reason this is being ignored and the old "Cannot redeclare" error occurs for files that are only included once in php4. For testing, I used the function_exits call to conditionally include the first function and the next function will then cause the error (none are system function names or even remotely reserved). Adding this code to hundreds of functions isn't very smart either when the entire file should not be executed again.

What's changed in php5 that causes this defined/define mechanic to unexpectedly fail? What needs to be changed to fix this?

NOTE: I was able to reproduce this error by requiring FileB and FileC in FileA, when FileC also requires FileB. I used the define/defined mechanic in all files with the appropriate values. I confirmed, with get_defined_constants, that the constants are being created as expected in both versions, but return is indeed not being passed back to the caller as expected in the php5 configuration.
 

Schluppy

Ars Tribunus Militum
2,486
Weird construct, I've never seen something like that before. Some comments for return suggest that returning in the global scope halts execution of the included file but not the processing.

I'm guessing that the continued processing in spite of the return may be what has changed between versions. To fix it I'd suggest using require_once, I really can't think of any other fix.
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
To make sure it is even hitting it, put a print statement before and after the return. _${FILENAME}.PHP may be defined locally and is not in the correct namespace when the next file gets to that code - we noticed this after we had to add some "global $var" statements to make things work without errors during our PHP4->5 migration; apparently in PHP4 it either added the vars to the global namespace, or was "smart" enough to figure it out automagically.

Of course, only spend your time on it if 'require_once()' fails for you.
 
Schluppy":2poua4ho said:
Weird construct, I've never seen something like that before. Some comments for return suggest that returning in the global scope halts execution of the included file but not the processing.

I'm guessing that the continued processing in spite of the return may be what has changed between versions. To fix it I'd suggest using require_once, I really can't think of any other fix.

That's what I was guessing as well - looks like I've no choice. It seems rather broken that a return doesn't work anymore, but the processing portion will actually skip a function_exists as expected.

For another test, I tried function_exists and return to see if processing is skipped and it behaved the same. It's ignoring return for processing and not defined, as said in the above comment you posted.
 
ronelson":hj29enni said:
To make sure it is even hitting it, put a print statement before and after the return. _${FILENAME}.PHP may be defined locally and is not in the correct namespace when the next file gets to that code - we noticed this after we had to add some "global $var" statements to make things work without errors during our PHP4->5 migration; apparently in PHP4 it either added the vars to the global namespace, or was "smart" enough to figure it out automagically.

Of course, only spend your time on it if 'require_once()' fails for you.

I was hardcoding the filename, so that wasn't a variable in the problem [pun intended].

Since I've already the define/defined mechanism set up, changing to require_once will make sure it's backwards and forwards compatible.
 
Status
Not open for further replies.