Re: [PHP] Nested functions
Thanks for the confirmations Torben. > > Can one code user-defined functions nested within one another > Yes. > > - and repeatedly execute them? > No. ;) > > This is correct. As you know, the thing is that execution keeps running > into that function declaration every time the containing function is > run, tries to declare the function, and fails. Essentially: while you > *can* do nested functions in PHP, don't. :) There really isn't any good > reason to, anyway--except for (perhaps) a wee bit of namespace > clarification. Namespace clarification is exactly what I had in mind - I am post-processing the n=0, 1, >1 results from a preg_match_all() and figured that array_walk() would be faster than for{$i...} or foreach(). However learning that array_walk() demanded use of a function, that implied that my 'modular' function would have to call a (short) second to suit the command structure - thus the logic that nesting would 'keep things together' in reuse/restructure situations. [As you will appreciate, the "test bed" code was almost directly out of the manual, and was only being used so that I could experiment/get my head around the command/and thereafter test the efficiency criteria] I thought I would ask, if only because other languages, even interpreted languages, do cope with this situation - but as you say, it is not a show-stopper - a possible case for FUNCTION_once name() as per REQUIRE and INCLUDE? Thanks again, =dn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Nested functions
On Sat, 2002-02-09 at 15:40, DL Neil wrote: > Hi, > Have been experimenting with the array_walk function and a little array debug print >facility, but ran into a > problem with nested functions. The code can be written with nested functions and >successfully executed once; but > when repeatedly executed an error appears. > > Can one code user-defined functions nested within one another Yes. > - and repeatedly execute them? No. ;) > The PHP Manual: Chapter 12. Functions, says: > "A function may be defined using syntax such as the following: > ... > Any valid PHP code may appear inside a function, even other > functions and class definitions." This is correct. As you know, the thing is that execution keeps running into that function declaration every time the containing function is run, tries to declare the function, and fails. Essentially: while you *can* do nested functions in PHP, don't. :) There really isn't any good reason to, anyway--except for (perhaps) a wee bit of namespace clarification. > Here is some test-bed code: > > Function ShowList( $title, $aList ) > { > > Function FnNested( $item, $key ) > { > echo "$key=" . $item . "~"; > } //end of FnNested > > echo "Listing array=$title: n=" . count( $aList ); > array_walk( $aList, "FnNested" ); > } //end of function ShowList > > //mainline code > $aLocated = array( 'zero', 'one', 'two', 'three' ); > ShowList( "Located1", $aLocated ); > ShowList( "Located2", $aLocated ); > ?> There really isn't any reason to do this; you could achieve the same thing like so: $key=" . $item . "~"; } //end of FnNested function ShowList( $title, $aList ) { echo "Listing array=$title: n=" . count( $aList ); array_walk( $aList, "FnNested" ); } //end of function ShowList //mainline code $aLocated = array( 'zero', 'one', 'two', 'three' ); ShowList( "Located1", $aLocated ); ShowList( "Located2", $aLocated ); ?> Hope this helps, Torben -- Torben Wilson <[EMAIL PROTECTED]> http://www.thebuttlesschaps.com http://www.hybrid17.com http://www.inflatableeye.com +1.604.709.0506 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Nested functions
Hi, Have been experimenting with the array_walk function and a little array debug print facility, but ran into a problem with nested functions. The code can be written with nested functions and successfully executed once; but when repeatedly executed an error appears. Can one code user-defined functions nested within one another - and repeatedly execute them? The PHP Manual: Chapter 12. Functions, says: "A function may be defined using syntax such as the following: ... Any valid PHP code may appear inside a function, even other functions and class definitions." Here is some test-bed code: $key=" . $item . "~"; } //end of FnNested echo "Listing array=$title: n=" . count( $aList ); array_walk( $aList, "FnNested" ); } //end of function ShowList //mainline code $aLocated = array( 'zero', 'one', 'two', 'three' ); ShowList( "Located1", $aLocated ); ShowList( "Located2", $aLocated ); ?> Sample output: FnShowList~ Listing array=Located1: n=4 0=zero~ 1=one~ 2=two~ 3=three~ FnShowList~ Fatal error: Cannot redeclare fnnested() in c:\data\webs\webute_tst\l.php on line 6 Please advise, =dn WinNT 4.0 SP6a running PHP vn 4.0.6, Apache vn 1.3.20WinNT, MySQL vn 3.23.40-nt -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] nested functions
>What about using classes instead of outer functions? Classes provide >rather nice (nestable) scoping. You're right! If I had written it from scratch classes would be the best way! Although, I'm a newbie and I haven't read the PHP object oriented capter yet ;-). Hannes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] nested functions
On Wednesday 16 May 2001 12:12, Hannes Schmiderer wrote: > elias wrote: > > good question...I might need an answer when you get one. > > Why? > Ok, I have a perl-counter script which is included to shtml-pages with > SSI. Now I want to add some PHP pages to my site. So I need a > PHP-function that does the same as the perl-script does. In the script > I have a lot of global variables and some auxiliary functions. In PHP > these variables became local variables of the counter() function and > the auxiliary functions became nested functions. The auxiliary nested > functions have to have access to a lot of variables from the counter() > function, and they even have to change them. I already added all the > variables (some with call by reference) but I think it would have been > easier if I had access the counter()-variables within the nested > auxiliary functions direct. What about using classes instead of outer functions? Classes provide rather nice (nestable) scoping. -- Christian Reiniger LGDC Webmaster (http://sunsite.dk/lgdc/) "Software is like sex: the best is for free" -- Linus Torvalds -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] nested functions
elias wrote: > good question...I might need an answer when you get one. Why? Ok, I have a perl-counter script which is included to shtml-pages with SSI. Now I want to add some PHP pages to my site. So I need a PHP-function that does the same as the perl-script does. In the script I have a lot of global variables and some auxiliary functions. In PHP these variables became local variables of the counter() function and the auxiliary functions became nested functions. The auxiliary nested functions have to have access to a lot of variables from the counter() function, and they even have to change them. I already added all the variables (some with call by reference) but I think it would have been easier if I had access the counter()-variables within the nested auxiliary functions direct. Hannes PS: I wonder why my original message does not appear in Outlook Express News-Folder... just the answers? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] nested functions
good question...I might need an answer when you get one. -elias ""Johannes Schmiderer"" <[EMAIL PROTECTED]> wrote in message 9ds7le$caa$[EMAIL PROTECTED]">news:9ds7le$caa$[EMAIL PROTECTED]... > Is there no way to access the $x within the inner function: > > function outer() > { > $x = 50; > function inner() > { >echo $x; > } > > inner(); > } > > Global in the inner function does not work. I do not want to have $x global > for all. > > Hannes Schmiderer > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] nested functions
why don't you pass $x as an argument? - Original Message - From: "Johannes Schmiderer" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: 2001. május 15. 23:41 Subject: [PHP] nested functions > Is there no way to access the $x within the inner function: > > function outer() > { > $x = 50; > function inner() > { >echo $x; > } > > inner(); > } > > Global in the inner function does not work. I do not want to have $x global > for all. > > Hannes Schmiderer > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] nested functions
Is there no way to access the $x within the inner function: function outer() { $x = 50; function inner() { echo $x; } inner(); } Global in the inner function does not work. I do not want to have $x global for all. Hannes Schmiderer -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]