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:
> <?php
> 
> Function ShowList( $title, $aList )
> {
> 
>  Function FnNested( $item, $key )
>  {
>     echo "<br>$key=" . $item . "~";
>  } //end of FnNested
> 
>  echo "<br>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:

<?php
error_reporting(E_ALL);

function FnNested( $item, $key ) {
    echo "<br>$key=" . $item . "~";
} //end of FnNested


function ShowList( $title, $aList ) {
    echo "<br>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

Reply via email to