|
Posted by Jason Barnett on 01/07/05 17:56
>>Bottom line: I always require_once at the top of a file for any files
>>that I will require. No sense in doing anything else if the required
>>files aren't there. For conditionally including files include_once()
>>has always made the most sense to me.
>
>
> I'm not sure that's the best strategy: since include() will continue
> executing your script even if the file does not exist, you shouldn't use
> include() if anything which follows will fail in the absence of the
> (non-)included code. If the content of the other file is essential to the
> correct operation of your script, you should use require(), since that will
> terminate with a fatal error if the required file is not present.
Isn't this what I said? :P Oh well, no biggie.
>
> It makes sense to require() files in a conditional branch if *only* the code
> in that branch is dependent on the required file. If all (or at least
> enough) branches rely on the file, then requiring it at the top of the file
> is a sensible move.
Ah, now I'm getting more what you mean. To be clear: I generally try to
split up my files into the lowest common denominator and keep
functionality seperated. The way that I delegate files I generally put
one class / module into one file; this just makes it easier for me to
manage code. So if I *need* a certain class / module for another class
/ module to work, then I just require_once at the top of the file.
>
> Whether you use the basic include()/require() or the _once versions depends
> on whether you want the code in the file to be executed each time the
> require/include is executed or not. For initialization of constants or
> parameter variables, the _once versions make sense; for code which contains
> substantive logic or output, it may not, especially if inside a loop.
>
Excellent point. Most of what I delegate to includes falls into the
categories of constant / variable initialization / class definition /
function definition. In fact I do it that way to prevent my logic
errors from include'ing the wrong way just as you have described.
--
Teach a person to fish...
Ask smart questions: http://www.catb.org/~esr/faqs/smart-questions.html
PHP Manual: http://php.net/manual/
php-general archives: http://marc.theaimsgroup.com/?l=php-general&w=2
Navigation:
[Reply to this message]
|