组织PHP类层次结构的最佳方法 [英] Best Way to Organize PHP Class Hierarchy

查看:130
本文介绍了组织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?

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

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?

类结构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 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.

推荐答案

真正要解决的是在应用程序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.

这篇关于组织PHP类层次结构的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆