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 "<br>value returnded by foo() ".$foooutput; > echo "<br>value $a after calling foo() but before calling bar() ".$a; > > $bar_output=bar(); > echo "<br>value returnded by bar() ".$bar_output; > . > . > echo "<br>value $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 no records. Typicaly display error message > ,repeat quyery with default values etc..... > } > if ($lines >1) > { > oops, database is inconsistent. exactly one line should be returned. Stop > script > die("database EXAMPLE TABLE have data inconsistency. contact server > admin now") > } > > if ($lines=1) > { > $retval= pg_fetch_array($result,0); // we have what we are looking for... > } > > } > else > { > die("No select from example_table") > } > //end of the validating code > return $retval; > } > > > function bar($data_1,$data_2,$data_3) > { > global $spojeni; > $sqlstring="select * from other_example_table where bla bla bla ...... some > thuing'"; > // bla bla bla and similar as in foo() > > > } > ?> > > > in traditional approach, it could be done better and flexibile only by few > and limited ways. > One of then is to use for example include "" like this: > > <? > 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 exactlz > 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 > > include "database_code.php" ; // all validating code is moved inside > database_code.php3 , so it could be uset more frequently in more functions ; > > //end of the validating code > > return $retval; > } > ?> > > but it have several caveats. For example , database_code.php can be a big > fat file with lot of lines and tests and if function foo() is called > somewhere from loop, it will be included each itteration and it menas, > that in each iterration will be parsed for errors (fix me if i am wrong...) > etc etc and loop can have hunderds or thousends itteration and i iffraid, it > could have a serious performance impact. > > > The code rewriten for using my suggested "explicitly setiing of variable > scope inside function" language extension, exactly "var_scope caller;" will > look like this ( the following example uses "get_var_scope()" function, that > is part of the sugested extensions to examine WHAT variable scope we are > currently have and "local_vars $varname" (with list variables, that are > intendet to be LOCAL ONLY , it act like oposite of "globals") ) : > > <? > > $database="postgres"; > . > . > . > . > do something > . > . > . > // now zou need a call subroutine > > $something=foo('cats','dogs','horses'); > . > $different=bar('mices','meat','gras'); > . > . > . > . > > function foo($data_1,$data_2,$data_3) > { > global $spojeni,$database; > . > . > 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'" > $retval= db_execute_query($sqlsting); > return $retval; > } > > > function bar($data_1,$data_2,$data_3) > { > global $spojeni; > $sqlstring="select * from other_example_table where mouse_catching='$data1' > AND sniffing='$data_2' AND riding='$data_3'"; > $retval= db_execute_query($sqlsting); > return $retval; > } > > > > function db_execute_query($sqlstring) > { > // executing database query, making all tests that everything went good > etc... simple database abstraction layer included as bonus :o) ...... > > var_scope caller; // ve inherit variable scope from where we are called > global $database; // we need name of the database engine > local_vars $i,$j,$k,$partial_result,$retval,$sqlstring ; // good for loop > counters, like for($i=0;$i<something;$i++),parameters and retvals, to do > not harmly interfere with function from we are called .it has opposite > meaning to "globals" > > $caller_name=get_var_scope(); // we need to know from where we are called > do construct error messages > > if ($database=="postgres") > { > > // database postgres specific code > $result=pg_exec($spojeni,$sqlstring); > if ($result) > { > $lines=pg_numrows($result); > if ($lines <1) > { > do something if we have no records. Typicaly display error message > } > if ($lines >1) > { > oops, database is inconsistent. Stop script > die("database inconsistency in function." .$caller_name." contact > server admin now") > } > > if ($lines==1) > { > $retval= pg_fetch_array($result,0); // we have what we are looking > for... > } > > } > else > { > die("No select from example_table in ".$caller_name) > } > } > > if ($database=="mysql") > { > // mysql specific code > > } > etc etc etc....... > > return $retval; > > > } > ?> > > > in the following example, simple and very "good readable" code is created, > with is smaller, flexibile and simple to underestand and i expect, it will > run faster. > > I thing, that my sugested langue extension can lead to more flexibile coding > style, allow to bulid modular programs far better, increse readibility of > php code and may lead to smaller and faster php programs in some very > common situations. So may be useful to add it to php language. > > > Abstract: > > Suggestion of adding capability "Explicit setting of variable scope inside > user defined functions". > adding "var_scope local", "var_scope global", and most useful "var_scope > caller" to explicitly set, what variable scope will be used inside user > defined function.Some supporting keywords and functions suggested too. > keyword "local_vars variable list" as oposite for "globals" to avoid > harm interference of private function varibles with variables of its > caller and function "get_var_scope()" to detect, witch variable scope is > currently used. I hope that this extension can lead to flexibile, shorter > and faster PHP code in common situations and is "worth of implementation" > > Thanx for reading my letter and i appologize for wasting your time (if > any).And execuse my bed english; > > > NTPT > > > --- > Odchozí zpráva neobsahuje viry. > Zkontrolováno antivirovým systémem AVG (http://www.grisoft.cz). > Verze: 6.0.401 / Virová báze: 226 - datum vydání: 9.10.2002 > > > -- > PHP Development Mailing List <http://www.php.net/> > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php