简体   繁体   English

组织PHP类层次结构的最佳方法

[英]Best Way to Organize PHP Class Hierarchy

I've got a somewhat primitive framework I've been using for most of my projects, but a general design issue came to mind that I haven't been able to work out yet. 我有一个原始的框架,我一直在使用我的大多数项目,但一般的设计问题浮现在脑海里,我还没有能够解决。 For a given application, should I separate the application-specific class structure from the framework's structure, or is building on top of the framework not such a bad thing? 对于给定的应用程序,我应该将特定于应用程序的类结构与框架的结构分开,还是构建在框架之上并不是一件坏事?

For example, say I had a framework with a base Controller class and extended it for a given part of my application. 例如,假设我有一个带有基本Controller类的框架,并将其扩展到我的应用程序的给定部分。 Which arrangement makes the most sense, and why? 哪种安排最有意义,为什么?

Class Structure A: 班级结构A:

  • Intuitive, easy to find source files when debugging. 调试时直观,易于查找的源文件。
  • File naming/directory structure mirrors class heirarchy. 文件命名/目录结构镜像类heirarchy。
- Framework_Control                 "Framework\Control.php"
   - Framework_Control_Index        "Framework\Control\Index.php"
   - Framework_Control_Home         "Framework\Control\Home.php"
   - Framework_Control_Contact      "Framework\Control\Contact.php"
   - Framework_Control_About        "Framework\Control\About.php"

Class Structure B: 班级结构B:

  • Keeps framework modular and easily to replace/update. 保持框架模块化,易于更换/更新。
  • Adds some complexity to directory structure, directory/file naming no longer follows class heirarchy all the time. 为目录结构增加了一些复杂性,目录/文件命名不再一直跟在类heirarchy之后。
- Framework_Control                 "Framework\Control.php"
   - Application_Control_Index      "Application\Control\Index.php"
   - Application_Control_Home       "Application\Control\Home.php"
   - Application_Control_Contact    "Application\Control\Contact.php"
   - Application_Control_About      "Application\Control\About.php"

I know overall it's going to come down to personal preference, but I want to make sure I weigh out all the pros and cons before I decide which way to go. 我知道整体而言,这将归结为个人偏好,但我想确定在决定走哪条路之前,我要权衡所有的利弊。 It really comes down to class naming and directory structure as the actual hierarchy would remain the same in either case. 它实际上归结为类命名和目录结构,因为实际的层次结构在任何一种情况下都保持不变。

I would suggest that you view your source code in two different categories, external dependencies or code that is used across multiple sites and not native to any single one and native dependencies or the code that is native to the particular site that you're working on. 我建议您在两个不同的类别中查看源代码,外部依赖项或跨多个站点使用的代码,而不是任何单个站点和本机依赖项本机,或者您正在处理的特定站点的本机代码。

It sounds like Framework/Control.php is part of a larger external dependency and should be managed as such, while the Application/Control files are all native the particular website. 听起来像Framework / Control.php是更大的外部依赖的一部分,应该这样管理,而应用程序/控制文件都是特定网站的原生。

Using this differentiation in our code structure has made it much easier to reuse our in-house framework between multiple sites very easily. 在我们的代码结构中使用这种差异使得在多个站点之间轻松地重用我们的内部框架变得更加容易。

As a final thought, you might consider looking at what the major frameworks out there are doing such as Zend Framework, Symfony and others. 最后,您可以考虑查看Zend Framework,Symfony等其他主要框架。 Even though the entire framework might be more than you want the structure of the frameworks can provide a lot of insights into common, good practices that are being used by PHP developers everywhere. 尽管整个框架可能比您想要的更多,但框架的结构可以为PHP开发人员在各地使用的常见,良好实践提供大量见解。

What it really comes down to is what are you going to do when you update Framework\\Control.php in Application XYZ. 它真正归结为您在Application XYZ中更新Framework \\ Control.php时要做什么。 Are you going to go back to Application ABC and make that same change? 你打算回到Application ABC并做同样的改变吗? What if it's a critical bug? 如果这是一个关键的错误怎么办?

For maintainability of all of your projects I'd go with your second option. 为了保证所有项目的可维护性,我会选择第二种方案。

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

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