Amy
Amy

Reputation: 133

call_user_func how to solve this error

How to solve this problem with call_user_func

When I call the function I have this error generated by php. Below the error.

Warning: call_user_func() expects parameter 1 to be a valid callback, function 'getChildsInMenuCount' not found or invalid function name i

the line inside my files with the function

 while ($Qcategories->fetch() ) {
    $categories_count++;
    $rows++;
    if ((!isset($_GET['cID']) && !isset($_GET['pID']) || (isset($_GET['cID']) && ((int)$_GET['cID'] === $Qcategories->valueInt('id')))) && !isset($cInfo) && (substr($action, 0, 3) != 'new')) {

      $category_childs = ['childs_count' => AdministratorMenu::getChildsInMenuCount($Qcategories->valueInt('id'))];

      $cInfo_array = array_merge($Qcategories->toArray(), $category_childs);
      $cInfo = new objectInfo($cInfo_array);
    }

The result of var_dump(__FUNCTION__); is string(20)"getChildsInMenuCount"

  class AdministratorMenu {
    // Count how many subcategories exist in a category
        public static function getChildsInMenuCount($id) {
          $OSCOM_Db = Registry::get('Db');

          $categories_count = 0;

          $Qcategories = $OSCOM_Db->prepare('select id
                                            from :table_administrator_menu
                                            where parent_id = :parent_id
                                            ');

          $Qcategories->bindInt(':parent_id', $id );
          $Qcategories->execute();

          while ($Qcategories->fetch() !== false) {
            $categories_count++;
            $categories_count += call_user_func(__FUNCTION__, $Qcategories->valueInt('id'));
          }

          return $categories_count;
        }
      }

Upvotes: 0

Views: 155

Answers (2)

MahdiY
MahdiY

Reputation: 1306

change call_user_func parameters like below:

call_user_func(__CLASS__ . '::' . __FUNCTION__, $Qcategories->valueInt('id'));

You must also specify the class in first parameter of call_user_func

Upvotes: 0

Barmar
Barmar

Reputation: 782683

Since it's a class method, you need to use __METHOD__, not __FUNCTION__. This will include the class prefix.

$categories_count += call_user_func(__METHOD__, $Qcategories->valueInt('id'));

Upvotes: 3

Related Questions