Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

include() failed...Or did it?

Status
Not open for further replies.

Itshim

Programmer
Apr 6, 2004
277
0
0
US
Doing some testing, I have found that when and include statement fails to open a stream it returns boolean FALSE (along with generating warnings), but an included file could also return FALSE. I would like to know if there is a way to determine (programmatically) if the include failed or it was successful and the file is returning FALSE.


-- -- -- -- -- -- -- --

If you give someone a program, you will frustrate them for a day
but if you teach them how to program, you will frustrate them for a lifetime.
 
Code:
if ((include $filename) === FALSE){

//do something
}
 
That is what I normally do, but it occurred to me this morning that an include file could have the statement:
Code:
return FALSE;
In which case the code:
Code:
if ((include $filename) === FALSE){

//do something
}
would not be able to determine if the file was included or not.

I am not actually having a problem seeing as none of my includes actually return anything, I just thought of the situation and was wondering if there was a way to detect what was happening.

I thought of adding something to a custom error handler but that seemed messy. You could check using class_exists() or function_exists() (if the file defines a class or a function). Actually that just gave me an idea, you could use the function get_included_files() and see if the file exists in the returned array... I guess I just answered my own question.

--Sorry for the train of thought post.


-- -- -- -- -- -- -- --

If you give someone a program, you will frustrate them for a day
but if you teach them how to program, you will frustrate them for a lifetime.
 
you're right. this, I think, is a bug in the include handling of php, the include itself worked so should not report false.

so i think you are left with two options:

1. use require instead
2. use the FALSE test PLUS test for an E_WARNING in error_get_last() (obviously you need to test for the particular line so as not to pick up a previous error).
 
Excellent, error_get_last() looks like the right tool for the job.

I have a tendency to stay away from require() calls, because if it fails it issues an fatal error (E_ERROR), problem with that being that there is no way to trap a fatal error.

I was hoping that require() would be changed to issue a E_RECOVERABLE_ERROR, which I feel better represents the state of the engine at fail time.

PHP Manual said:
E_RECOVERABLE_ERROR - Catchable fatal error. It indicates that a probably dangerous error occured, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handle (see also set_error_handler()), the application aborts as it was an E_ERROR.

I know I'm probably alone on this because the actual definition of the word 'require' is: to have need of; need.

Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top