简体   繁体   中英

Error when calling function inside the function

I'm making a class that has several functions. There is one thing that makes me confused, which is when I call the function itself in its function:

in the following function, I don't get an error when I call the getChildKey function inside the function itself:

    function getChildKey1($FPTree)
{
  function getChildKey($FPTree){
    $result = [];
    if (isset($FPTree['child'])){
      foreach ($FPTree['child'] as $key => $value){
        $result[$key]=getChildKey($value);
      }
    }
    if (empty($result)){
      return 'gak ada array';
    }
    return $result;
  }
  $output = [];
  foreach ($FPTree as $index => $child){
    $output[$index]=getChildKey($child);
  }
return $output;
}

and I get an error when I try to create a function similar to the getChildKey function, which calls the function itself in it:

function pathFinder($arr = [], $needle)
{

    $path = '';
    $paths = [];

    foreach ($arr as $key => $value) {

        if (is_array($value)) {
            $path .= $key . "->";
            pathFinder($value, $needle); //error here


        } else {
            if ($key === $needle) {
                $paths[] = $path . $key; 

            }
        }
    }
    return $paths; // return all found paths to key $needle
}

Why did it happen? what do I have to do so that the pathFinder function can call itself in it?

When you define a function, and call it that time (if function was found) php creates a scope which will be like a different "house" for all variables and functions that are declared/defined inside it. That's the reason we sometimes need to use the keyword global when we want to reference $var1 which is defined outside the function (and class if any) and we happen to have a variable with the same name declared in our function

CASE 1:

function A($args1)   
{ 
   //---- scope of A starts here
   function B($args2)
   { return 'hello from B'; }

    B();   // call B

   //---- scope of A ends here
}

CASE 2:

function A($args1)   
{ 
   //---- scope of A starts here

    A();   // call A
    //but where is A?

   //---- scope of A ends here
}

In 1st case the A function's block (anything between { }) is self-contained block meaning it has everything that any code inside it would need in order to run. So when B() is encountered php looks within the scope of A , whether any function named B is defined? Yes!

In 2nd case PHP does the same when it encounters A() but on not finding it there, it looks in the global scope ie. all code that is outside of your class to find a function named A . Since there isn't any it throws a not found exception

When you use $this->A() php knows where to look for this function ie.inside the same object where it was called from and it run perfectly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM