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

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条)

报告相同问题?