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

发布于 2024-07-07 18:55:52 字数 1169 浏览 4 评论 0原文

我有一个在我的大多数项目中都使用的有点原始的框架,但是我想到了一个一般的设计问题,但我还没有能够解决。 对于给定的应用程序,我应该将特定于应用程序的类结构与框架的结构分开,还是在框架之上构建并不是一件坏事?

例如,假设我有一个带有基本控制器类的框架,并将其扩展为我的应用程序的给定部分。 哪种安排最有意义,为什么?

类结构A:

  • 直观,调试时很容易找到源文件。
  • 文件命名/目录结构反映了类层次结构。
- 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"

类结构B:

  • 保持框架模块化并且易于替换/更新。
  • 给目录结构增加了一些复杂性,目录/文件命名不再始终遵循类层次结构。
- 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'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. Which arrangement makes the most sense, and why?

Class Structure A:

  • Intuitive, easy to find source files when debugging.
  • File naming/directory structure mirrors class 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:

  • 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.
- 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

碍人泪离人颜 2024-07-14 18:55:52

我建议您以两种不同的类别查看源代码:外部依赖项或跨多个站点使用的代码,而不是任何单个站点的本机代码;本机依赖项或您正在处理的特定站点的本机代码。

听起来 Framework/Control.php 是更大的外部依赖项的一部分,应该这样管理,而应用程序/控制文件都是特定网站的本机文件。

在我们的代码结构中使用这种差异使得在多个站点之间重用我们的内部框架变得更加容易。

最后,您可能会考虑看看主要框架正在做什么,例如 Zend Framework、Symfony 等。 尽管整个框架可能超出您的预期,但框架的结构可以提供许多关于各地 PHP 开发人员正在使用的常见、良好实践的见解。

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.

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. 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.

〆凄凉。 2024-07-14 18:55:52

真正的问题是,当您更新应用程序 XYZ 中的 Framework\Control.php 时,您要做什么。 您要返回应用程序 ABC 并进行相同的更改吗? 如果这是一个严重的错误怎么办?

为了您所有项目的可维护性,我会选择您的第二个选择。

What it really comes down to is what are you going to do when you update Framework\Control.php in Application XYZ. Are you going to go back to Application ABC and make that same change? What if it's a critical bug?

For maintainability of all of your projects I'd go with your second option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文