开发者

PHP Best Practices : Im loading the $GLOBALS array with a lot of information to be shared between my php pages

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 开发者_JS百科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


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.


No, it's a very messy practice.

No, its got nothing to do with MVC.

A fundamental concept of modular programming (which underpins object-oriented programming and overlaps significantly with functional programming) is that global variables are bad - an awful lot of what constitutes good programming (and good programming languages) is all about isolating parts of the system from other parts.

That you are storing the data in global variables does nothing to help your objective of making the information available to all php files.

By all means group them under, say, a single global array, that way you don't have to keep passing it around in function/method calls.

One approach to populating maintaining the common data is to add implement processing of it withni a custom session handler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜