-2

So I'm trying to do something like this:

function func() {

    $call = function() {
        ...
    };

    $call();

}

But it's throwing an error saying:

Function name must be a string

I also tried calling the function like this:

$this->call();
call(); // and like this

And it's not working as well.

Is there a reason why I can't do what I'm doing?

EDIT

It seems to be a problem with the original code, and not in the example I wrote

This is my real code:

$data = [...];
$menu_array = [];
$getChildren = function($id) {
          $children = [];
          foreach ($data as $node) {
              if ($id == $node["parent"]) {
                  array_push($children, $node);
              }
          } 
          return empty($children) ? null : $children;
        };

        $check = function($arr, $dat) {
            foreach ($dat as $node) {
                $children = $getChildren($node["id"]);
                if ($children == null) {
                    $arr[$node["display_name"]] = $node["model"];
                } else {
                    $arr[$node["display_name"]][] = $children;
                    $check($children);
                }
            }
        };
$check($menu_array, $data);

The error is thrown in this line:

$children = $getChildren($node["id"]);
FightRay
  • 144
  • 14
  • Look at the line number in error. – revo Jan 10 '19 at 13:55
  • ¯\\_(ツ)_/¯ It [works](https://imgur.com/a/4VAJbSl) on my computer (php 7.2.4) – Cid Jan 10 '19 at 13:55
  • @revo The line number is where $call() is at. – FightRay Jan 10 '19 at 13:56
  • @Cid Doesn't work on debian with PHP 7.0.33 – FightRay Jan 10 '19 at 13:58
  • It's still working on windows 10 with php 7.0.29 – Cid Jan 10 '19 at 13:59
  • 3
    Can you share the exact code you are running and the exact error message? – Nico Haase Jan 10 '19 at 14:03
  • Your code looks fine. Are you sure you are executing the correct version of php? – k0pernikus Jan 10 '19 at 14:05
  • Are you using this in web application or console? if CLI, php path might be the wrong one and use the 5.x version – Cid Jan 10 '19 at 14:09
  • @NicoHaase Updated the post with my original code – FightRay Jan 10 '19 at 14:11
  • 1
    Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Nico Haase Jan 10 '19 at 14:18
  • @FightRay I haven't given your post a single down vote because I think that this is an interesting question. But the other question provides some background of the technical difficulties you are facing, and it clearly tells you about the possibilities to use `use`. So please don't be rude – Nico Haase Jan 10 '19 at 14:40
  • @NicoHaase Yeah that may be helpful but you're going as far as to call it a duplicate, when it's not even the same question. You can't even find it by searching for my question. It's a completely different case... that's why I think saying possible duplicate is rude. If you think it's an interesting question, you should not call it a duplicate. – FightRay Jan 10 '19 at 14:42
  • Possible duplicate of [Anonymous recursive PHP functions](https://stackoverflow.com/questions/2480179/anonymous-recursive-php-functions) – rob006 Jan 10 '19 at 15:03
  • @FightRay You're missing the point of marking things as duplicate here - that people have many different ways of running across (and searching for) the same problem. Duplicates are sign posts to the answer. That you couldn't find it by searching for your problem is the issue marking a duplicate is intended to solve. – user3942918 Jan 11 '19 at 00:08
  • As for the current close reason (lacking a [MCVE]) - those votes came in prior to you editing the question to include code that actually demonstrated the problem. Of the 5 votes needed to put a question on hold it already had the majority share, and so won out. The lack of code demonstrating the problem also probably explains downvotes you received, and people don't generally stick around waiting for a question to be fixed up in order to retract a vote. – user3942918 Jan 11 '19 at 00:23

1 Answers1

2

What you want to do here, is recursion! The problem is, that PHP does not automatically add any variables from the outer scope, into the function scope. In your code $check($children);, the variable $check is actually not defined.

You can fix this by telling PHP that it should use the $getChildren and $check variable from outside the function:

$getChildren = function($id) use (&$getChildren) {
   ...

and

$check = function($arr, $dat) use (&$check, &$getChildren) {
  ...

Adapted from https://stackoverflow.com/a/2480242/2681964

Michiel Dral
  • 3,932
  • 1
  • 19
  • 21
  • Great answer, thank you. – FightRay Jan 10 '19 at 14:30
  • @FightRay If it worked out, could you mark this as accepted answer? That helps my reputation, you reputation and the fact that this worked for you for people that come here later :) – Michiel Dral Jan 10 '19 at 14:32
  • Yeah of course, I did it. And the other question someone claimed is a possible duplicate is not even close to similar to mine... I think my question was helpful since I couldn't find any turn of events of the sort in google and I explained my issue pretty well. – FightRay Jan 10 '19 at 14:36