Re: Suggestion: global variables being accessed in localscope

From: Date: Fri, 09 Feb 2007 02:57:32 +0000
Subject: Re: Suggestion: global variables being accessed in localscope
Groups: php.internals 
Request: Send a blank email to internals+get-27933@lists.php.net to get a copy of this message
Christian Schneider wrote:
Please use plain text mail as your messages are a PITA to read, thanks.
I changed the email to send/recieve messages. Hotmail simply doesn't accept plain text.
You didn't grasp two of the major PHP features (not bugs!): 1. Every variable you access inside a function is local unless explicitely imported (through $GLOBALS, global or superglobals) from the global scope. 2. Accessing undefined variables is normal and not an error (not everybody agrees here but lots of people consider this a big plus). Changing this would turn PHP into a completely different language so it's simply not going to happen. If you don't like these basic concepts it's probably a good idea to switch language.
First of all, I sleep with PHP codes printed everyday... I can't live without PHP!!!! =D Forget about your suggestion to switch the language! Exactly. And I also commented it in my blog ("Maybe this suggestion is currently too much difficult to achieve.")... this is a drastic change! I suggested a similar change, that does the same thing. My suggestion: Implement a register_superglobal function, that add the variable in a HashTable... something like this (remember... Compiler class scope): function register_superglobal( $varName ) { global $$varName; $this->symbolTable->registered_superg->hash[ $varName ] = $$varName; } And then... in your variable retrieval (I wrote the code in PHP now... you scared me about that language switch!)... instead of the current implementation (I removed the fatal error, based on your comment: "Accessing undefined variables is normal and not an error"): // Retrieve variable from local scope $v = $this->symbolTable->getInLocal( $this->lexer->getStringValue() ); if ( $v === null ) { // ... } To something like this: // Retrieve variable from local scope $v = $this->symbolTable->getInLocal( $this->lexer->getStringValue() ); if ( $v === null ) { // Try to retrieve the variable from registered "superglobal" hash $v = $this->symbolTable->registered_superg->hash[ $this->lexer->getStringValue() ]; if ( $v === null ) {
       // ...
} } As you can see... just a little change, and now I do not change the whole PHP behavior! And I enabled the possibility to add the function I suggested. The performance impact is minimal (almost 0). If you do not accept my idea, I'll understand. I am just trying to help PHP to be a better and language and also help lots of PHP developer like to make code easier to write. =) Best regards, -- Guilherme Blanco - Web Developer CBC - Certified Bindows Consultant Cell Phone: +55 (16) 9166-6902 MSN: guilhermeblanco@hotmail.com URL: http://blog.bisna.com São Carlos - SP/Brazil

« previous php.internals (#27933) next »