简体   繁体   English

OOP PHP 项目中的 Includes vs Classes 文件夹有什么区别?

[英]What is the difference between Includes vs Classes folder in OOP PHP project?

I am trying to create php site using object oriented programming and smarty template.我正在尝试使用面向 object 的编程和 smarty 模板创建 php 站点。

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?我见过有些人制作 Includes 文件夹和 Classes 文件夹,有些人将 Classes 放在 Includes 文件夹中,哪种方式是正确的? What is includes folder for in object oriented programming? object 面向编程的包含文件夹是什么?

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)此外,您的自动装载机应符合PSR-0标准(由 PHP 标准工作组编写)

And example of a PSR-0 compliant autoloader is available at https://gist.github.com/221634 https://gist.github.com/221634上提供了符合 PSR-0 的自动加载器示例

There is no correct way, and no textbook standard.没有正确的方法,也没有教科书的标准。 Folder structures in your PHP project are up to you to organize however you like. PHP 项目中的文件夹结构由您自行组织,但您喜欢。 Different people go by different conventions.不同的人 go 有不同的约定。

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.也许有些人称它为includes ,因为它包含通常包含在大多数其他主要脚本中的文件,有些人称它为classes ,因为它包含 class 文件。 Since class files are included files, perhaps they make classes a subfolder of incliudes for that reason.由于incliudes文件是包含文件,因此可能它们使classes成为包含的子文件夹。 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.据说没有标准文件夹,但includes/是相当主流的,有些人可能会认为这是事实上的标准。 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.但是,您的应用程序应该遵循的更重要的原则是 MVC(模型-视图-控制器)架构。 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.对于 MVC 应用程序,您的类的作用通常只是封装您的应用程序的数据 model。 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.在为 OO PHP 构建包含时,我会从 Java 中获得灵感。 For each class that has a dependency on another class use the following statement at the top of the file:对于依赖于另一个 class 的每个 class,请在文件顶部使用以下语句:

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.如果找不到所需的文件,则会导致致命的 E_COMPILE_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.换句话说,它会停止脚本,而 include_once() 只会发出一个警告 (E_WARNING) 允许脚本继续。

Using this structure will mean that all class files have all the required includes specified, so there will be some duplication eg some classes may require_once the same file.使用这种结构意味着所有 class 文件都指定了所有必需的包含,因此会有一些重复,例如某些类可能需要同一个文件。 However because of the way require_once works this carries little overhead and makes the code very resilient to change.然而,由于 require_once 的工作方式,这带来的开销很小,并且使代码对更改非常有弹性。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM