douba8758 2011-07-19 07:46
浏览 23
已采纳

PHP最佳实践:我正在加载$ GLOBALS数组,其中包含许多要在我的php页面之间共享的信息

my question is about whether what I'm doing is proper , or if it causes security / performance issues.

Im using the $GLOBALS array , and filling it with a lot of values from the database, such as (page title, user information, template information ... etc) so that all my php files have access to this info. Im following the MVC design pattern, so this is how my View files have access to the information generated in the Model files.

If this is not the proper way to do things, please tell me why, and what is the proper way.

Thank you

  • 写回答

2条回答 默认 最新

  • doumi7854 2011-07-19 08:03
    关注

    You're is not following the MVC pattern, you are just using global variables to display something in a "shared all"-script in a "shared nothing"-architecture.

    Doing output can mean you get a view of something, but normally that is not to be mixed with the term View of the MVC (Model-View-Controller) pattern. What you do, templating or output is maybe a better wording.

    If this is not the proper way to do things, please tell me why, and what is the proper way.

    If you want to implement MVC pattern, than this might be the MVC you're referring to:

    • Model: Your Filesystem and Database
    • View: Your HTML output + CSS + JS
    • Controller: Your PHP Script

    MVC is just a pattern. You can implement it like you feel good. And you can find it in lot of stuff that has implemented nothing. It's just a pattern.

    But more precisely the MVC pattern is more commonly seen for an object oriented implementation of it. Your implementation is totally different to such of a object oriented one, you're using global state instead of object instances.

    But you have not written in your question that you thought about implementing an object oriented MVC pattern and that templating script you use is part of it.

    It can be perfectly proper to do it like you do it depending on the needs to fulfil.

    MVC is just a pattern. It has pro's and con's. It has benefits and it has consequences. It's just a pattern.

    Especially if you're concerned about performance don't make things more complicated as they need to be, e.g. loading a full blown object tree to just display some variables inside a HTML template.

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

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值