php 框架中的全局变量开销

发布于 2024-12-03 07:22:40 字数 741 浏览 0 评论 0原文

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

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

  • 在我看来,这种方法不是真正的面向对象方法
  • IDE (netbeans) 无法为 Core 类的对象提供文档 - 这是一个痛苦将使用该框架的开发人员。
  • 我真的很担心性能问题 - 不知道global是否更慢或者其他什么。

我已经搜索过&没有找到任何有关性能问题的信息。我还搜索了 stackoverflow &发现 使用全局会产生任何开销吗? & PHP 中全局变量和函数参数之间的优点/缺点? 等链接,但它们不包含太多信息。 现在我主要关心的是性能,所以请帮忙。

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:

  • 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄凉少年不暖心 2024-12-10 07:22:40

我必须同意 NevilleK,你是 Core`类声音与 God Object反模式。

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

全局状态与性能无关(尽管在 php 中它有一些较小的影响),但它创建了不可测试且紧密耦合的代码。

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


为您提供的一些其他视频:

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.

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.

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:

梦开始←不甜 2024-12-10 07:22:40

我通过为 Agile Toolkit 创建不同的模式解决了类似的问题.org/learn/understand/base/adding" rel="nofollow">添加对象并在系统范围内使用它。这会将属性传递给新创建的名为“api”的对象,该对象始终引用应用程序类

应用程序类并不是真正的上帝类,但它将各种功能委托给系统控制器、页面等。此截屏视频解释了非常基本的对象是如何构造的,它可能也是您正在寻找的东西:

http://www.youtube.com/watch?v=bUNEHqYVOYs

I've solved a similar problem in Agile Toolkit by creating a different pattern for adding object and using it system-wide. This passes a property to a newly created objects called "api" which always references Application class.

Application class is not really a God class but it delegates all sorts of functionality to system controllers, pages etc. This screencast explains how the very basic objects are structured, it might be something you are also looking for:

http://www.youtube.com/watch?v=bUNEHqYVOYs

苍暮颜 2024-12-10 07:22:40

首先,当您关心性能时,您可能需要阅读 http://en.wikipedia.org/wiki /God_object 首先 - 你的“核心”类听起来像一个“God object”,这是一个相当完善的反模式。

就性能而言 - 找出答案的最佳方法是对其进行测试。如果您正在编写一个框架,我假设您正在编写单元测试来验证它的行为;扩展单元测试以包含简单的性能指标并不难。您还可以使用 JMeter 或类似工具投资测试脚本,以练习使用该框架构建的一些页面的“参考实现”。通过这样做,您将获得更好的关于您的具体情况的信息,而不是尝试根据 Stack Overflow 对一般工作方式的集体知识来优化设计。

一般来说,我想说,只要没有做太多工作,拥有一个全局类就不会对性能产生太大影响。简单地将类加载到内存中、解析它等确实会对性能产生影响 - 但它不太可能比您可能采取的任何其他路线慢得多。

但是,如果您的“核心”类在页面访问时执行大量初始化逻辑,那么它显然会影响性能。

Firstly, whilst you are concerned with performance, you may want to read http://en.wikipedia.org/wiki/God_object first - your "core" class sounds like a "God object", which is a fairly well-established anti pattern.

In terms of performance - the best way to find out is to test it. If you're writing a framework, I'm assuming you're writing unit tests to validate it's behaviour; it's not hard to extend that unit testing to include simple performance metrics. You might also invest in test scripts using JMeter or similar to exercise a "reference implementation" of a few pages built using the framework. You'll get far better information on your specific situation from doing this than by trying to optimize the design based on Stack Overflow's collective knowledge of the general way things work.

In general, I'd say that having a global class shouldn't impact performance too much, as long as it isn't doing much work. Simply loading the class into memory, parsing it, etc. does have a performance impact - but it's not likely to be measurably slower than any other routes you might take.

If, however, your "core" class does lots of initialization logic when it's accessed by the page, it's obviously going to impact performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文