Re: [PHP-DEV] Idea to extend language: Explicitly setting variablescope inside user defined function (longer)
Personally, I think that variable scope handling works great the way it is--particularly if you turn on error reporting. This way you have to explicitly declare that you will be accessing a global variable and don't run the risk of messing things up without thinking about it. You could argue that a bit of discipline could solve this problem--but then again a bit of discipline also makes you use the scoping the way it is and come away clean without any major problems! --Mt. On Mon, 2002-10-21 at 21:19, Hans Zaunere wrote: --- Marco Tabini [EMAIL PROTECTED] wrote: Well, you have to admit that the issue of variable scope is the first thing that hits someone who approaches PHP for the first time and comes from other backgrounds, like C or ASP! Still, after one adapts to this apparent weirdness of scoping, it tends to grow on you. I find that no scope inheritance gives me one less thing to worry about when writing code... While some of the scoping tricks proposed seem like potential overkill, I've yearned for a way to explicitly declare a variable a super-global. Sure, I can stick it in one of the predefined super-globals, but that just seems wrong. Something like: super $avar; would be very useful for large projects and wouldn't cause a lot of harm otherwise. Hans Marco On Sun, 2002-10-20 at 22:41, Rasmus Lerdorf wrote: How in the world do you know that code will run faster ? Implementing your suggestion would take quite a lot of changes to the internals of PHP. Instead of just a global and a current symbol table, we would now need basically an unlimited number of symbol tables and every variable lookup would become more complex. Ergo it would slow things down. -Rasmus On Mon, 21 Oct 2002, NTPT wrote: Hi. I have some idea and suggestion how to extend PHP language a bit in some way. That may probably lead to increasing of php flexibility, allow more modular coding to be done etc My sugestion is simple: Allow PHP programmer to explicitelly told , WHAT variable scope will be used inside user defined functions. In the traditional approach (afaik , i use php 4.2.2 ), as is described in the manual of php there a diferent varable scopes for each functions , only syntax global $valuename ; can lead to use variables global. This aproach is traditional and well known and is sufficient for most tasks.(I say sufficient, not effective...). My idea is going a bit behind it. I suggest to introduce new keyword(s) or function(s) into the PHP language definiton (i suggest syntax like var_scope scope or var_scope(scope) ) That keywords SHOULD be used in user defined functions to EXPLICITLY define, WHAT kind of variable scope will be used inside this function. scope can be either 'local' = it means, that all variables used in this function have a local scope only.(it means like traditional behavior of php and its variable scopes until now ) 'global' = each variable used in the function is from global scope. Similar to global $variable_1,$variable_2... $each variable used in the main execution line of the script 'caller' or 'inherit' This is MOST USEFUL part of the idea . Function variable scope is the SAME as from where the function was called. (if functino bar(),with have var_scope set to 'caller', is called from function foo() it have the same variable scope as function foo(), almost like the code of function bar() was included (by include something ) somewhere inside foo() ) A little example code for demonstrating idea of the syntax and how it should work: */ ? $a=10; echo Varaible $a.$a; . $foo_output =foo(); echo brvalue returnded by foo() .$foooutput; echo brvalue $a after calling foo() but before calling bar() .$a; $bar_output=bar(); echo brvalue returnded by bar() .$bar_output; . . echo brvalue $a after calling bar() .$a; function foo() { $a=20; echo $a inside function foo() = .$a /* $bar_inside foo = bar(); echo Variable $a inside function foo() after calling bar().$a; */ . . return $a; } function bar() { var_scope caller // we have the SAME variable scope as from where we are called $a=100; . . . return $a; } ? this should return : (with comments behind // ) Varaible $a 10 $a inside function foo() = 20 value returnded by foo() 20 value $a after calling foo() but before calling bar() 10 value returnded by bar() 100 value $a after calling bar() 100 // var_scope is set to 'caller', so $a in global scope is modified inside bar() the some
Re: [PHP-DEV] Idea to extend language: Explicitly setting variablescope inside user defined function (longer)
How in the world do you know that code will run faster ? Implementing your suggestion would take quite a lot of changes to the internals of PHP. Instead of just a global and a current symbol table, we would now need basically an unlimited number of symbol tables and every variable lookup would become more complex. Ergo it would slow things down. -Rasmus On Mon, 21 Oct 2002, NTPT wrote: Hi. I have some idea and suggestion how to extend PHP language a bit in some way. That may probably lead to increasing of php flexibility, allow more modular coding to be done etc My sugestion is simple: Allow PHP programmer to explicitelly told , WHAT variable scope will be used inside user defined functions. In the traditional approach (afaik , i use php 4.2.2 ), as is described in the manual of php there a diferent varable scopes for each functions , only syntax global $valuename ; can lead to use variables global. This aproach is traditional and well known and is sufficient for most tasks.(I say sufficient, not effective...). My idea is going a bit behind it. I suggest to introduce new keyword(s) or function(s) into the PHP language definiton (i suggest syntax like var_scope scope or var_scope(scope) ) That keywords SHOULD be used in user defined functions to EXPLICITLY define, WHAT kind of variable scope will be used inside this function. scope can be either 'local' = it means, that all variables used in this function have a local scope only.(it means like traditional behavior of php and its variable scopes until now ) 'global' = each variable used in the function is from global scope. Similar to global $variable_1,$variable_2... $each variable used in the main execution line of the script 'caller' or 'inherit' This is MOST USEFUL part of the idea . Function variable scope is the SAME as from where the function was called. (if functino bar(),with have var_scope set to 'caller', is called from function foo() it have the same variable scope as function foo(), almost like the code of function bar() was included (by include something ) somewhere inside foo() ) A little example code for demonstrating idea of the syntax and how it should work: */ ? $a=10; echo Varaible $a.$a; . $foo_output =foo(); echo brvalue returnded by foo() .$foooutput; echo brvalue $a after calling foo() but before calling bar() .$a; $bar_output=bar(); echo brvalue returnded by bar() .$bar_output; . . echo brvalue $a after calling bar() .$a; function foo() { $a=20; echo $a inside function foo() = .$a /* $bar_inside foo = bar(); echo Variable $a inside function foo() after calling bar().$a; */ . . return $a; } function bar() { var_scope caller // we have the SAME variable scope as from where we are called $a=100; . . . return $a; } ? this should return : (with comments behind // ) Varaible $a 10 $a inside function foo() = 20 value returnded by foo() 20 value $a after calling foo() but before calling bar() 10 value returnded by bar() 100 value $a after calling bar() 100 // var_scope is set to 'caller', so $a in global scope is modified inside bar() the some way, as if global $a was used ) if you uncomment lines in bar() Varaible $a 10 $a inside function foo() = 20 Variable $a inside function foo() after calling bar() 100 // var_scope in function bar() is set to caller, so $a IN SCOPE of foo() ONLY is modified inside bar(). value returnded by foo() 100 value $a after calling foo() but before calling bar() 10 // var_scope in function bar() is set to caller, so $a IN GLOBAL SCOPE is NOT modified inside bar(). value returnded by bar() 100 value $a after calling bar() 100 // var_scope is set to caller, so $a in global scope is modified inside bar() the some waz, as if global $a was used For WHAT it could be useful ?? for example for writing a databaze extraction layer of any aplication. common real life situations are similar like in this example code (simplified and abstracted from real life code of PHP application I currently write ) ? . . . . do something . . . // now you need a call subroutine $something=foo('cats','dogs','horses'); . $different=bar('mices','meat','gras'); . . . . function foo($data_1,$data_2,$data_3) { global $spojeni; . . do something here with $data_1,$data_2,$data_3. . . . $sqlstring=select * from example_table where mouse_catching='$data1' AND sniffing='$data_2' AND riding='$data_3' $result=pg_exec($spojeni,$sqlstring); // code, that validate if db done that we want, if select return exactly one line or if there is a some data inconsistrency etc etc. The following code (wery simplified snipplet from real life situation) is almost the same inside many of the functions if ($result) { $lines=pg_numrows($result); if ($lines 1) { do something if we have
Re: [PHP-DEV] Idea to extend language: Explicitly setting variablescope inside user defined function (longer)
At 04:31 AM 10/21/2002 +0200, NTPT wrote: Hi. I have some idea and suggestion how to extend PHP language a bit in some way. That may probably lead to increasing of php flexibility, allow more modular coding to be done etc My sugestion is simple: Allow PHP programmer to explicitelly told , WHAT variable scope will be used inside user defined functions. In the traditional approach (afaik , i use php 4.2.2 ), as is described in the manual of php there a diferent varable scopes for each functions , only syntax global $valuename ; can lead to use variables global. This aproach is traditional and well known and is sufficient for most tasks.(I say sufficient, not effective...). My idea is going a bit behind it. I suggest to introduce new keyword(s) or function(s) into the PHP language definiton (i suggest syntax like var_scope scope or var_scope(scope) ) That keywords SHOULD be used in user defined functions to EXPLICITLY define, WHAT kind of variable scope will be used inside this function. scope can be either 'local' = it means, that all variables used in this function have a local scope only.(it means like traditional behavior of php and its variable scopes until now ) 'global' = each variable used in the function is from global scope. Similar to global $variable_1,$variable_2... $each variable used in the main execution line of the script 'caller' or 'inherit' This is MOST USEFUL part of the idea . Function variable scope is the SAME as from where the function was called. (if functino bar(),with have var_scope set to 'caller', is called from function foo() it have the same variable scope as function foo(), almost like the code of function bar() was included (by include something ) somewhere inside foo() ) I think having something like caller is very bad. It would mean that functions don't have any contracts anymore as they can start messing around with the callers symbol table. Andi -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php