在PHP项目中,存储,访问和组织辅助对象有什么样的模式? [英] In a PHP project, what patterns exist to store, access and organize helper objects?

查看:150
本文介绍了在PHP项目中,存储,访问和组织辅助对象有什么样的模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于PHP的面向对象的项目中,如何组织和管理辅助对象,如数据库引擎,用户通知,错误处理等?



说我有一个庞大的PHP CMS。
CMS按各种类组织。一些例子:




  • 数据库对象

  • 用户管理

  • 用于创建/修改/删除项目的API

  • 一个消息对象,向最终用户显示消息

  • 一个需要你的上下文处理程序

  • 可以显示按钮

  • 一个日志对象

  • 错误处理



等。



我正在处理永恒的问题,如何最好地使这些对象可以访问到需要它的系统的每个部分。



我的第一个apporach,很多年前是一个包含初始化实例的$应用程序全局的这些课程。

  global $ application; 
$ application-> messageHandler-> addMessage(Item successfully inserted);

然后我更改为Singleton模式和出厂功能:

  $ mh =& factory(messageHandler); 
$ mh-> addMessage(Item successfully inserted);

但我也不满意。单元测试和封装对我来说变得越来越重要,在我理解全球/单身人士背后的逻辑破坏了OOP的基本概念。



然而当然,给予每个对象一些指向需要的帮助对象的指针的可能性,可能是最干净,资源节约和测试友好的方式,但是我对这一长期的可维护性有疑问。



大多数PHP框架我已经研究了使用单例模式或访问初始化对象的函数。两个很好的方法,但正如我所说,我也不高兴。



我想扩大我在这里存在什么常见模式的视野。我正在从长期现实世界透视图中寻找有关资源的例子,其他想法和指针。



另外,我有兴趣听到有关专业,利基或奇怪的方法来解决问题。

解决方案

我会避免Flavius建议的Singleton方法。有很多理由来避免这种做法。它违反了良好的OOP原则。谷歌测试博客有一些关于单身人士的好文章,以及如何避免:



http://googletesting.blogspot.com/2008/08/by-miko-hevery-so-you- join-new-project.html
http://googletesting.blogspot.com/2008/05/tott-using-dependancy-injection-to.html
http://googletesting.blogspot.com/2008/08/where-have-all-singletons-gone.html



替代方案




  1. 服务提供商



    http:// java。 sun.com/blueprints/corej2eepatterns/Patterns/Ser viceLocator.html


  2. 依赖注入



    http://en.wikipedia.org/wiki/Dependency_injection



    和php说明:



    http://components.symfony-project.org/dependency-injection/trunk/book/01-Dependency-注入


这是一篇关于这些替代方法的好文章:



http://martinfowler.com/articles/injection.html



实现依赖注入( DI):




  • 我相信你应该询问公司需要什么对象的功能的构造方法 new YourObject($ dependencyA,$ dependencyB);


  • p>您可以手动提供所需的对象(依赖关系) $ application = new Application(new MessageHandler())。但您也可以使用 DI框架(维基百科页面提供链接到PHP DI框架)。



    重要的是,您只传递实际使用的内容(调用操作),而不是传递给其他对象,因为它们需要它。这是Bob叔叔最近发表的一篇文章(罗伯特·马丁),讨论了手动DI与使用框架




更多关于Flavius解决方案的想法。我不希望这个帖子成为一个反帖子,但我认为重要的是看看为什么依赖注入是至少对我来说比全局变量更好。



甚至虽然这不是一个真实的 Singleton 实施,我仍然认为弗拉维修错了。 全球状态不好。请注意,此类解决方案也使用难以测试静态方法



我知道很多人这样做,批准并使用它。但是阅读Misko Heverys博客文章( Google可测试专家),重新阅读并慢慢消化他所说的话确实改变了我看到设计的方式。



如果你想能够测试你的应用程序,你需要采用不同的方法来设计你的应用程序。当您进行测试优先编程时,您将遇到以下困难:接下来我要实现这段代码的日志记录;让我们先写一个测试记录一个基本的消息',然后提出一个测试,迫使你写和使用一个不能被替换的全局记录器。



我仍然挣扎我从该博客获得的所有信息,并不总是易于实现,我有很多问题。但是,在掌握了Misko Hevery所说的话之后,没有办法回到以前做过的事情(是的,全球国家和Singletons(大S)): - )


How do you organize and manage your helper objects like the database engine, user notification, error handling and so on in a PHP based, object oriented project?

Say I have a large PHP CMS. The CMS is organized in various classes. A few examples:

  • the database object
  • user management
  • an API to create/modify/delete items
  • a messaging object to display messages to the end user
  • a context handler that takes you to the right page
  • a navigation bar class that shows buttons
  • a logging object
  • possibly, custom error handling

etc.

I am dealing with the eternal question, how to best make these objects accessible to each part of the system that needs it.

my first apporach, many years ago was to have a $application global that contained initialized instances of these classes.

global $application;
$application->messageHandler->addMessage("Item successfully inserted");

I then changed over to the Singleton pattern and a factory function:

$mh =&factory("messageHandler");
$mh->addMessage("Item successfully inserted");

but I'm not happy with that either. Unit tests and encapsulation become more and more important to me, and in my understanding the logic behind globals/singletons destroys the basic idea of OOP.

Then there is of course the possibility of giving each object a number of pointers to the helper objects it needs, probably the very cleanest, resource-saving and testing-friendly way but I have doubts about the maintainability of this in the long run.

Most PHP frameworks I have looked into use either the singleton pattern, or functions that access the initialized objects. Both fine approaches, but as I said I'm happy with neither.

I would like to broaden my horizon on what common patterns exist here. I am looking for examples, additional ideas and pointers towards resources that discuss this from a long-term, real-world perspective.

Also, I'm interested to hear about specialized, niche or plain weird approaches to the issue.

解决方案

I would avoid the Singleton approach suggested by Flavius. There are numerous reasons to avoid this approach. It violates good OOP principles. The google testing blog has some good articles on the Singleton and how to avoid it:

http://googletesting.blogspot.com/2008/08/by-miko-hevery-so-you-join-new-project.html http://googletesting.blogspot.com/2008/05/tott-using-dependancy-injection-to.html http://googletesting.blogspot.com/2008/08/where-have-all-singletons-gone.html

Alternatives

  1. a service provider

    http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html

  2. dependency injection

    http://en.wikipedia.org/wiki/Dependency_injection

    and a php explanation:

    http://components.symfony-project.org/dependency-injection/trunk/book/01-Dependency-Injection

This is a good article about these alternatives:

http://martinfowler.com/articles/injection.html

Implementing dependency injection (DI):

Some more thoughts on Flavius's solution. I don't want this post to be an anti-post but I think it's important to see why dependency injection is, at least for me, better than globals.

Even though it is not a 'true' Singleton implementation, I still think Flavius got it wrong. Global state is bad. Note that such solutions also use difficult to test static methods.

I know a lot of people do it, approve it and use it. But reading Misko Heverys blog articles (a google testability expert), rereading it and slowly digesting what he says did alter the way I see design a lot.

If you want to be able to test you application, you'll need to adopt a different approach to designing your application. When you do test-first programming, you'll have difficulty with things like this: 'next I want to implement logging in this piece of code; let's write a test first that logs a basic message' and then come up with a test that forces you to write and use a global logger that can't be replaced.

I am still struggling with all the information I got from that blog, and it's not always easy to implement, and I have many questions. But there's no way I can go back to what I did before (yes, global state and Singletons (big S)) after I grasped what Misko Hevery was saying :-)

这篇关于在PHP项目中,存储,访问和组织辅助对象有什么样的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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