Re: Idea to extend language: Explicitly setting variable scope inside user defined function (longer)

From: Date: Mon, 21 Oct 2002 16:14:41 +0000
Subject: Re: Idea to extend language: Explicitly setting variable scope inside user defined function (longer)
References: 1 2 3 4  Groups: php.dev 
Request: Send a blank email to php-dev+get-89806@lists.php.net to get a copy of this message
----- Original Message ----- From: "Andrey Hristov" <andrey@webgate.bg> To: "Marco Tabini" <marcot@tabini.ca> Cc: <php-dev@lists.php.net> Sent: Monday, October 21, 2002 12:42 PM Subject: Re: [PHP-DEV] Idea to extend language: Explicitly setting variable scope inside user defined function (longer) > > > > Marco Tabini <marcot@tabini.ca> 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! > > > > For me global vars are bad at all. > If you want to use some value in a function then pass it to the function > even if you > want you can pass an array will variable ammount of vars that has names as > indexes. Nice idea, i use it sometimes, it works, but ...... real life situation like this: <? $sqlstring=" UPDATE testtable SET db_value1='$value_1',db_value1='$value_1' .....etc db_value_n='$value_n' "; // now you must produce $passed_variables array , like this // glue code $passed_variables['value_1']=$value_1; $passed_variables['value_2']=$value_2; . etc . $passed_variables['value_n']=$value_n; // end of glue code $result = db_query($passed_variables,$sqlstring) { // inflat passed variables to local scope variables. // start of glue code reset($passed_variables); while(current($passed_variables)!==FALSE ) { $valuename=key($passed_variables); $$valuename=current($passed_variables); next($pole); } } // end of glue code do something with passed variables... } ?> so it have a lot of glue code, that do nothing but only glue together twoo parts of the program . if you write a big application, with a lot of queries and function calls, a lot of the glue code is nedded, that need to be executed, look ugly and decrease readability of the program. And this is exactly the situation, I wnat avoid by my suggestion. > Only the input must be global and it is global by using > _GET/POST/REQUEST/FILES/SESSION Right. There is a little variables, that need to be truly GLOBALS. GPRFS and db connection id for example. > With the PHP approarch you know which vars are defined in the function's > scope only by looking > the "global" statement. > I agree that this is hard for newbie my suggestion is not mentioned to be a newbie helper. It is intended for avoiding need of glue code(like in example aobve )and/or redundant code, that can not be insert inside procedures because current var scope model. >- it was hard for me, but now I don't > have problems with that and look above. > if I eventualy need a global var I use $GLOBALS and not "global $foobar"; > 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

« previous php.dev (#89806) next »