dsm0688 2011-09-08 09:05
浏览 50
已采纳

框架中的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.

  • 写回答

3条回答 默认 最新

  • doulangpeng3933 2011-09-08 10:22
    关注

    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:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答