Suggestion: global variables being accessed in local scope
| From: | Guilherme Blanco | Date: | Thu, 08 Feb 2007 17:32:38 +0000 |
| Subject: | Suggestion: global variables being accessed in local scope | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-27919@lists.php.net to get a copy of this message | ||
Hello PHP maintainers,During this week, I spoke with Sara Golemon about the possibility to change
one current behavior of PHP.My first suggestion, as you can see in my blog post: http://blog.bisna.com/archives/32#comments was
a new function called register_superglobal.While talking with her, I found that it'll be easier
to be implemented another thing.Currently, when we deal with objects and functions in a PHP
application, to access most common variables, we have to do one of these things:function m() {
global $DB; // ...}Another one:function m() { $DB = Database::getInstance(); // ....}Or:function
m() { $DB = Registry::get("DB"); // ...}And finally... via Factory:function m() {
$DB = Env::getDatabaseConnection(); // ...}We can also specify via runkit a superglobal variable
(DB), and use it.While all those solutions have some costs, maybe there is a possibility to change
the structure and simplify all those lines of code.As I already said, my first idea was a function,
register_superglobal, which you register it and access it in a local scope without using none of the
examples I listed.Sara told me about superglobals and etc, but I suggested her about the possibility
to change the idea a little. Here is the comment I describe 2 possible ideas:"The perfect
behavior should be (inside a function), if a variable
does not exist, try to access a global (without doing via $GLOBALS or
global). If it doesn’t exist in the second try, then throw a fatal
error. With this, you do not need to think in superglobal variable or
anything else. Maybe this suggestion is currently too much difficult to achieve.
The workaround can be done using normal variables. But, as I
mentioned before, when the statement uses a variable, it checks for the
local variables declarations hash table; the idea is different here… if
the variable does not exist in local, look in the hash table I
mentioned (which stores the subscribed variables to behavior like
superglobal) and, if found, grab the variable from the global variable
declarations hash table.
As you can see, there will be only one performance impact (in normal
variable), doing a hash table checking. You do not need to change
autoglobal or static variables. In an average system, the needed time
to do this checking will be an advantage than the currently best
approach (Registry). You lose in one side, but receive more in the
other."She agreed with my idea ("That’s all doable, and with autoglobals being CVs now,
it’s even doable
without too much performance impact on the global fetches themselves…"), BUT she said I will
have to convince engine team to accept this idea too. Also, she said that will be a too much added
complexity for too little. I disagree with her, since currently there are much more processment
retrieving one object from a Registry or using a global accessor than another compiler hash
checking. The performance impact is almost irrelevant (cannot be measured) and the final solution is
cleaner than all the others. If you look at real world large applications, you can experience these
workarounds. And, as I checked with my contact list (around 600), it'll be a great improvement
of PHP.Now I am leaving this suggestion to internals list, and I hope you look affectionately at
it.Best regards,Guilherme Blanco - Web Developer
CBC - Certified Bindows Consultant
Cell Phone: +55 (16) 9166-6902
URL: http://blog.bisna.com
MSN: guilhermeblanco@hotmail.com
São Carlos - SP/Brazil
_________________________________________________________________
O Windows Live Spaces está aqui! Descubra como é fácil criar seu espaço na Web e sua rede
amigos.
http://spaces.live.com/signup.aspx