Best Way to Organize PHP Class Hierarchy(组织 PHP 类层次结构的最佳方法)
问题描述
我的大部分项目都使用了一个有点原始的框架,但我想到了一个通用的设计问题,我还没有解决.对于给定的应用程序,我应该将应用程序特定的类结构与框架的结构分开,还是在框架之上构建不是一件坏事?
例如,假设我有一个带有基本 Controller 类的框架,并为我的应用程序的给定部分扩展了它.哪种安排最有意义,为什么?
类结构 A:
- 直观,调试时易于查找源文件.
- 文件命名/目录结构反映了类层次结构.
类结构 B:
- 保持框架模块化,易于替换/更新.
- 为目录结构增加了一些复杂性,目录/文件命名不再遵循类层次结构.
我知道总体而言这将取决于个人喜好,但我想确保在决定走哪条路之前权衡所有利弊.这实际上归结为类命名和目录结构,因为无论哪种情况,实际的层次结构都将保持不变.
真正归结为当你在应用程序 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 类层次结构的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:组织 PHP 类层次结构的最佳方法
基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01