I am trying to create php site using object oriented programming and smarty template.
Just to make it by book how should i build the structure?
I've seen some people make Includes folder and Classes folder and some put Classes inside Includes folder, which way is correct? What is includes folder for in object oriented progr开发者_Go百科amming?
You should use Autoloading to load your classes.
Also your autoloader should comply to the PSR-0 standard (written by the PHP Standards Working Group)
And example of a PSR-0 compliant autoloader is available at https://gist.github.com/221634
There is no correct way, and no textbook standard. Folder structures in your PHP project are up to you to organize however you like. Different people go by different conventions.
Perhaps some people call it includes
because it consists of files that are normally included in most other main scripts, and some people call it classes
because it consists of class files. Since class files are included files, perhaps they make classes
a subfolder of incliudes
for that reason. But all this is entirely subjective and preferential; it is by no means a standard.
There is no standard folder, per say, but includes/
is fairly mainstream, and some might argue it's a defacto standard. It really is up to you.
There is only one rule: it has to make sense. Seriously. Code must be readable and maintainable. The name of the topmost folder has little to do with that. How you organize your classes under that folder is far more important. As a personal preference, I'd use "classes" or "model" since that's what the contents of the file is. "Includes" reminds me of procedural programming era.
However, far more important principle your application should adhere to is MVC (model-view-controller) architecture. I recommend you to read up on that topic. There are also several good frameworks for that so that you won't need to write boilerplate code.
The thing with frameworks is that often you will have little choice about naming your folders since they already have strong beliefs about the "correct" way of doing things. That also makes the naming issue you brought up less relevant.
In case of MVC applications, the role of your classes will often only be to encapsulate the data model of your app. That is why I often use "model" as the name of the folder, not classes nor includes.
I would take inspiration from Java when structuring your includes for OO PHP. For each class that has a dependency on another class use the following statement at the top of the file:
require_once 'classFolder/SomeClass.php'
This statement will check if the file has already been included, and if so, not include (require) it again. If the required file cannot be found though it will result in a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include_once() will only emit a warning (E_WARNING) which allows the script to continue.
Using this structure will mean that all class files have all the required includes specified, so there will be some duplication e.g. some classes may require_once the same file. However because of the way require_once works this carries little overhead and makes the code very resilient to change.
Hope this helps.
精彩评论