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

查看:29
本文介绍了组织 PHP 类层次结构的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

类结构 A:

  • 直观,调试时易于查找源文件.
  • 文件命名/目录结构反映了类层次结构.
<上一页>- Framework_Control框架Control.php"- Framework_Control_Index "FrameworkControlIndex.php"- Framework_Control_Home "FrameworkControlHome.php"- Framework_Control_Contact "FrameworkControlContact.php"- Framework_Control_About "FrameworkControlAbout.php"

类结构 B:

  • 保持框架模块化,易于替换/更新.
  • 为目录结构增加了一些复杂性,目录/文件命名不再遵循类层次结构.
<上一页>- Framework_Control框架Control.php"- Application_Control_Index "ApplicationControlIndex.php"- Application_Control_Home "应用控制Home.php"- Application_Control_Contact "ApplicationControlContact.php"- Application_Control_About "ApplicationControlAbout.php"

我知道总体而言这将取决于个人喜好,但我想确保在决定走哪条路之前权衡所有利弊.这实际上归结为类命名和目录结构,因为无论哪种情况,实际的层次结构都将保持不变.

解决方案

真正归结为当你在应用程序 XYZ 中更新 FrameworkControl.php 时你将要做什么.您是否要返回应用程序 ABC 并进行相同的更改?如果这是一个严重的错误怎么办?

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

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                 "FrameworkControl.php"
   - Framework_Control_Index        "FrameworkControlIndex.php"
   - Framework_Control_Home         "FrameworkControlHome.php"
   - Framework_Control_Contact      "FrameworkControlContact.php"
   - Framework_Control_About        "FrameworkControlAbout.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                 "FrameworkControl.php"
   - Application_Control_Index      "ApplicationControlIndex.php"
   - Application_Control_Home       "ApplicationControlHome.php"
   - Application_Control_Contact    "ApplicationControlContact.php"
   - Application_Control_About      "ApplicationControlAbout.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.

解决方案

What it really comes down to is what are you going to do when you update FrameworkControl.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天全站免登陆