|
|||
|
According to this article: PHPit - Totally PHP Using globals in PHP
Global variables have the following problems: 1. Reusing parts of the script is impossible If a certain function relies on global variables, it becomes almost impossible to use that function in a different context. Another problem is that you can’t take that function, and use it in another script. 2. Solving bugs is much harder Tracking a global variable is much harder than a non-global variable. A global variable could be declared in some obscure include file, which could take hours to find, although a good text editor / IDE could help with this. 3. Understanding the code in a year will be much more difficult Globals make it difficult to see where a variable is coming from and what it does. You might know about every global during development, but after a year or so you’ll probably have forgotten at least half of them, and then you’ll be kicking yourself for using so many globals. Then the author proposes Registry as the eventual solution to global variables… But I really don’t see how the Registry pattern really solves the above 3 problems. You still can’t reuse part of the code if the objects aren’t created and stored in the Registry first, just like the global variables have to be created first. You still will run into bugs if you don’t take care of your Registry well. You still will find the code difficult to understand if you don’t remember where you are processing your Registry. None of the problems caused by global variables is really solved here, Registry Pattern is fundamentally identical to global variables, and it really seems the “improvement” is more so the mental perception than actuality. I'm posting it here so people who are knowledgeable can perhaps shine some light on solid reasons that Registry Pattern is actually as good as people say it is? |
|
|||
|
In my opinion zend_registry is for persisting information between instances of the script running. Like for storing database connection info, constant data that will not change during the course of the script running.
|
|
|||
|
Ron,
PHP within itself is per request based and not multi-threaded like java or other things. Zend_Registry in no way enables you to share data across multiple separated scripts(unless you are talking about includes, but it doesn't seem that way). The only way to persist data in PHP is to store information in a database, and Zend_Registry isn't the answer here... I've written more on this subject, Tangfucius If I can find good answers, it really would be great. |
|
|||
|
Sorry, I didn't say that right. I was just referring to using it with an ini file, like an include. As I am a newbie to php I jumped in over my head. I think I will regress and stick to lurking. Since I did post I will add my two cents, It looks like zend_registry is just an alternative to using global variables, but it is just that, an alternative. It allows you to keep your global data in a class that has the scope of a global. Maybe that is what the article is suggesting. In any event I know I am going to learn something of interest here.... back to lurking!
|
|
||||
|
Quote:
Registry - Patterns For PHP PHPit - Totally PHP Using globals in PHP
__________________
Zend Framework Resources: Zend Webinars | Reference Manual | API Docs | Books | FreeNode: #zftalk Getting Started Tutorials: Getting started with ZF | Getting started with Zend Auth |
|
|||
|
thanks, that's one of the better explanations I've seen, and I've posted it at my blog. Tangfucius
|
|
|||
|
I think one of the key differences is you can programmatically track any changes to the data since it goes through set/get functions. You will also have less variables, so you can view the data as whole much better than the entire $GLOBALS array.
It also has a more semantic meaning - you are getting and setting data from the registry which is common data accross your application. Generally with globals, they are part of the bulk of the script (procedural), but with this, it's a much smaller, which means much less (probably nil unless you are being stupid) chance of collision, which is the real risk with globals. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|