PHP框架OOP模型 [英] PHP Framework OOP model
问题描述
我认为我是经验丰富的程序PHP程序员.我已经实施了一些更大的项目.现在,我想在较小的版本上尝试OOP PHP(用于数据库导入/导出的框架+用户身份验证).由于我从未在这样的项目上尝试过OOP,所以我在对象设计方面遇到了问题.
I think I am experienced procedural PHP programmer. I've implemented a few bigger projects. Now I would like to try OOP PHP on lesser one (framework for DB import/export + user authentication). Since I've never tried OOP on such a project I have problem with object design.
我想以这种方式实现框架,我只需要创建对象的一个实例即可使用整个框架.我还想使代码安排得井井有条,所以我不会为所有方法/属性仅实现一个类.
I would like to implement the framework the way, I'll just need to create one instance of the object to use whole framework. I would also like to keep the code well arranged, so I won't implement only one class for all methods/properties.
我如何将一个大类分成几个较小的类,以使它们有条理(在更多的php文件中)?
How could I split one big class in to few lessers to keep them organized (in more php files)?
我认为我必须实现一个基类,然后使用其他基类对其进行扩展.但是那样,我将有许多小班,而不是大班.
I think I'll have to implement one base class and then extend it using the others. But that way I'll have many of small classes, not big one.
您将如何解决此问题?
我希望我能很好地解释问题.
I hope I explained the problem well.
感谢您的帮助.
推荐答案
我已经编写并维护了庞大的整体库多年,自从我离开该库以来,我感到更加高兴.请听我的建议:将您的应用程序分解为尽可能合理的小类.这对组织和内存使用都很有好处(一个巨大的PHP类定义会占用很多RAM,而RAM是PHP脚本中有限的资源).
I have written and maintained huge monolithic libraries for many years, and I'm way happier since I moved away from it. Take my advice: Break your application down into as small classes as possibly makes sense. It's good for organizing, as well as memory usage (a huge PHP class definition can eat up a lot of RAM, and RAM is a limited resource in a PHP script).
使用PHP 5的 自动加载 仅加载当前上下文中所需的那些类的机制.
Use PHP 5's autoloading mechanism to load only those classes thart you need in the current context.
我认为> Zend Framework 的组织和构建方式非常好.您可能想看看他们如何设计他们的类,以及他们如何在框架中组织大量功能.
I think the way Zend Framework is organized and built is quite good. You may want to take a look at how they designed their classes, and how they organized the immense amount of functionality in the framework.
As for how to organize tools and helper objects and libraries, I asked that question a few weeks back and got very good feedback. I'm still not done reading it, actually.
这篇关于PHP框架OOP模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!