Cakephp3 using recursive function in view cells

I want to use this Recursive Function to convert a php array into an array of parent and childs.

I have a this Cell:

class MenuCell extends Cell {
  public function display($menu) {

    $menus = $this - > Menus - > find('all', [
      'contain' => ['MenuItems']
    ]) - > where(['id' => $menu]);

    $menus = $this - > buildTree($menus);


    $this - > set(compact('menus'));
    $this - > set('_serialize', ['menus']);

  }

  public function buildTree($ar, $pid = null) {
    $op = array();
    foreach($ar as $item) {
      if ($item['parentId'] == $pid) {
        $op[$item['id']] = array(
          'name' => $item['name'],
          'parentId' => $item['parentId']
        );
        // using recursion
        $children = $this - > buildTree($ar, $item['id']);
        if ($children) {
          $op[$item['id']]['children'] = $children;
        }
      }
    }
    return $op;
  }


}

But it gives me this error:

However if i do the same thing in controller, It works very fine.

Any help would save my day.