I am a complete novice at this wonderful PHP stuff, so please bare with me. Working with PHP has inspired me to begin delving into other wonderful things like database design, C++, Java, etc.. My initial research into some of these other languages has led me to begin organizing and structuring my work differently.
Ideally, I would like to keep each class and subclass definition in its own seperate file and link to them as needed, or perhaps at times include a file which simply includes a collection of includes, thus creating a library of sorts. I am wondering how far you can actually push that before disk reads become noticable and/or a problem (assuming we are on a relatively decent server)?
Also, I am curious about the philosophical and technical differences between using include() versus require(). What would be a common reason to include a file that you were not sure if it existed or not? Why would you not first do an is_file() check, or some such, before attempting to include a file which you know full well may not be there? Would there be any disadvantages to requiring files that you have every reason to expect to be there?
Questions About Includes
Moderator: General Moderators
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
There shouldnt be anything noticable about including many files and it just helps to keep things a lot neater and easier to maintain/edit.
Yes you should really check to see if the file exists before you include it or else theres not much point including itif its not there, but then again if its all your work, then you should know that a file that you want to include needs to be there and so you always ensure that it is - so you dont need to check for it.
The difference between the two functions is that if include() fails then the script will continue to run, whereas it will stop if require() fails. Meaning that you should propbably require() most files as they will hold functions/classes that are vital to the loading/running of the script.
Other functions that you should consider are include_once() and require_once() which checks to make sure the file hasnt already been included (no need to include it more than once), this is good for your class/function files as you dont want them to be defined again.
Yes you should really check to see if the file exists before you include it or else theres not much point including itif its not there, but then again if its all your work, then you should know that a file that you want to include needs to be there and so you always ensure that it is - so you dont need to check for it.
The difference between the two functions is that if include() fails then the script will continue to run, whereas it will stop if require() fails. Meaning that you should propbably require() most files as they will hold functions/classes that are vital to the loading/running of the script.
Other functions that you should consider are include_once() and require_once() which checks to make sure the file hasnt already been included (no need to include it more than once), this is good for your class/function files as you dont want them to be defined again.
Re: Questions About Includes
This has long been a concern on the development list and at numerous times they've considered various scenarios that help cut back on the systems overhead that surrounds an include or recquire. As a matter of fact PEAR considered making this part of how they do things but that idea was squashed after some people voiced there concers.lolpix wrote: Ideally, I would like to keep each class and subclass definition in its own seperate file and link to them as needed, or perhaps at times include a file which simply includes a collection of includes, thus creating a library of sorts. I am wondering how far you can actually push that before disk reads become noticable and/or a problem (assuming we are on a relatively decent server)?
Now I agree with kettle that most of the time you aren't going to see or notice the overhead associated with this, but in a production environment this is one of those places that can easily be cleaned up. Additionally, if you are worried about the parse time associated with including things you don't need, use an accellerator. Ion Cube, Turck Mmcache, Zend Optimizer are all good. Turck is especially cool as it allows you to store data in system memory and provides an API for getting that data at successive script calls.
Persistence of a sort.
Cheers,
BDKR