0

I have the following php code:

  function mapWordsToOriginal()
  {
    try
    {
        $d = array();
        function map_FilteredWithOriginal($n, $m)
        {
            return(array(($n + 1) => ($m + 1)));
        }
        foreach ($this->wordsArray as $word)
        {

            array_push($d, array_map(
                             "map_FilteredWithOriginal", 
                             array_keys(
                               $this->wordsArray, $word), 
                             array_keys($this->originalText, $word)));
        }

        for ($i = 0; $i < count($d); $i++)
        {
            for ($j = 0; $j < count($d[$i]); $j++)
            {
                $this->mapFilteredWithOriginal +=$d[$i][$j];
            }
        }

        if ($this->debug)
        {
            print_r($this->mapFilteredWithOriginal);
        }
    }
    catch (Exception $e)
    {

        echo 'Caught exception: ', $e->getMessage(), "\n";
    }
}

i use this function couple of times (depends on how long is the text), however when I get to this code inside for the second time:

 function map_FilteredWithOriginal($n, $m)
    {
        return(array(($n + 1) => ($m + 1)));
    }

the script just stops.

I dont know why and no exception is being catched....

Ori Price
  • 3,593
  • 2
  • 22
  • 37

3 Answers3

2

You should add a function_exists() === false conditional around the function declaration so it will not be declared again upon the second pass. For what it's worth, you probably should typically not be declaring functions in a nested manner unless you really intend to declare functions conditionally (i.e. using a function to conditionally create a function).

You should just move the nested function outside of the main function in this case.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • if i'll move it outside, how this code will recognize it? array_push($d, array_map("map_FilteredWithOriginal", array_keys($this->wordsArray, $word), array_keys($this->originalText, $word))); it uses the method name, if its outside it wont recognize it... – Ori Price Jul 26 '12 at 19:50
  • The PHP parser parses in all function declaration prior to execution. As long as the function declaration is somewhere within the set of files included in the execution path. So it could be located at the bottom of the script and referenced at the top of the script and still work. – Mike Brant Jul 26 '12 at 19:52
  • If you intend this map_FilteredWithOriginal to be a method on a class (which wasn't clear from the snippet you put. Then it would be a method at the class level and would need to be referenced as $this->map_FilteredWithOriginal – Mike Brant Jul 26 '12 at 19:57
0

You are probably getting an error because on the second call to mapWordsToOriginal you would be re-declaring map_FilteredWithOriginal.

There is no reason to define a function inside a function in this case, as it doesn't limit scope and makes things less readable.

If you must define it as such, you can use PHP's function_exists function to only declare map_FilteredWithOriginal once.

Rusty Fausak
  • 7,355
  • 1
  • 27
  • 38
0

You cant catch an exception there.

Your code produces an fatal error because of redeclaring map_FilteredWithOriginal() by calling mapWordsToOriginal() more than once.

if ( FALSE === function_exists( 'map_FilteredWithOriginal' ) )
{
    function map_FilteredWithOriginal( $n, $m )
    {
        return( array( ( $n + 1 ) => ( $m + 1 ) ) );
    }
}

This will help i think.

Try to use error_reporting() in your projects (development time only), this makes debugging easier. :)

error_reporting( E_ALL | E_STRICT );

Have fun.

Raisch
  • 821
  • 6
  • 13