PHP框架中的全局变量开销 [英] php global variable overhead in a framework

查看:89
本文介绍了PHP框架中的全局变量开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个框架,该框架使用Core类的对象(此类具有强大的功能&使框架正常工作).该框架遵循MVC架构和具有松散耦合的Model,Control,View类.这些类需要大量引用Core类.到目前为止,我所做的是:创建Core类&的单个对象.在模型,控件,视图类中通过PHP关键字global对其进行引用.

I'm currently developing a framework which uses an object of a Core class (this class has huge functionality & makes the framework working). The framework follows MVC architecture & has loosely coupled Model, Control, View classes. Theses classes need a reference to the Core class heavily. What I've done so far is: creating single object of the Core class & referencing to it by PHP keyword global in Model, Control, View classes.

我不喜欢使用这种方法,主要是因为:

I don't like using this approach mainly because:

  • 在我看来,这种方式不是真正的面向对象的方式
  • IDE(netbeans)无法为Core类的对象提供文档-这将使使用此框架的开发人员感到痛苦.
  • 我真的很担心性能问题-不知道global是慢还是什么.
  • This way is not true object oriented way in my sense
  • The IDE (netbeans) can't provide documentation to the object of the Core class - a pain for developers who will be using this framework.
  • I'm really worried about performance issues - have no idea whether global is slower or whatever.

我已经搜索了&找不到有关性能问题的任何信息.我还搜索了stackoverflow&找到使用global是否会产生任何开销?& PHP中的全局变量和函数参数之间的优缺点?等链接,但它们包含的信息不多. 目前,我主要关注的是性能,请提供帮助.

I've searched & did not find any information regarding performance issue. I've also searched stackoverflow & found Does using global create any overhead? & The advantage / disadvantage between global variables and function parameters in PHP? etc links but they don't contain much information. Right now my main concern is performance, so please help.

推荐答案

我必须同意 God Object 反模式.

I must agree with NevilleK, that you Core` class sounds line an God Object antipattern.

对于任何愚蠢的建议使用单例/注册表的人,我建议对这个问题进行一些研究.它们创建与您的经典全局变量相同的全局状态.

And for anyone dumb enough to suggest use of singletons/registries i would suggest to do a little research on the subject. They create the same global state as your classical global variables.

全局状态与性能无关紧要(尽管在php中影响很小),但是它创建了无法测试且紧密耦合的代码.

Global state is not so much matter of performance ( though in php it has some minor impact ), but it created untestable and tightly coupled code.

您确实应该研究依赖注入.这可能会向您显示另一种方式,该方式不需要您的代码中具有这样的Core类.

You really should look into the Dependency Injection. That might show you another way , which does not require to have such a Core class in your code.

为您提供一些其他视频:

Some additional videos for you:

  • Global State and Singletons
  • Don't Look For Things!
  • Advanced OO Patterns
  • Cake is a Lie
  • Clean Code: Arguments

这篇关于PHP框架中的全局变量开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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