开发者

OOP in PHP: About static use in MVCs [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to开发者_C百科 be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

I've seen multiple guys who are talking about MVC as a scalable solution for a web application. But I've got some questions about MVC. I've read on multiple sites that using static objects leads to procedural programming and not that's far away from MVC.

Do you think is this true? If so, would using singleton patterns solve this problem?


I think Symfony2 has solved the issue quite elegantly. Instead of declaring static classes, it uses so-called "services" - there's a central manager, which contains all the services and their configurations. In different parts of your code you could get the services from the container. The container always hands out the same instance of any class required (unless specified otherwise in the configuration), which means that the singleton pattern is hardly ever needed.

The official docs make a lot better job explaining it, so here's the link about Symfony's Service Container.

I bet other frameworks also use similar methods to get rid of having the need to use singletons.


using static objects leads to procedural programming and that's far away from MVC

This makes no sense. MVC is a methodology to separate functionality of the application into discrete layers. Using an MVC design has nothing to do with whether or not your objects are static, or whether you use singletons. In fact, there's nothing that says an MVC design even has to be object oriented.

Most comments regarding the use of singletons deal with the difficulty in writing unit tests for them. See https://stackoverflow.com/questions/3876951/why-is-it-hard-to-unit-test-singletons for more details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜