返回的数组被赋值给 PHP 中的变量

returned array being assigned as null to variable in PHP

我在 PHP 中有一个函数,它调用另一个 class 函数来为其中一个变量 $related 赋值。

原来需要取值的函数使用requestACLForAccess()函数将特定变量filter添加到$headers变量中:

listRecords函数:

    public function listRecords($api)
{
    header("Access-Control-Allow-Origin: *");
    if ($this->authenticationProcedure() !== false) {
        $headers = apache_request_headers();
        $access = $this->requestACLForAccess($headers['module'], $headers['oauth_token'], $_SERVER['REQUEST_METHOD']);
        if(gettype($access) == "array"){
        $headers['filter'][0][$access[1]]['$equals'] = $access[2];
        }
        $listObject = new FilterApi();;
        return $listObject->filterList($api, $headers);
    }
}

正在调用函数 requestACLForAccess :

protected function requestACLForAccess(
    $targetModule,
    $token,
    $req_type,
    $getSpecialFlag = false,
    $targetId = null
) {
    $row = $this->checkTokenValid("token", $token);
    if (!empty($row) || $row !== false) {
        $related = \Sugarcrm\Sugarcrm\custom\clients\base\CustomACL::checkRelated($row['module'], $targetModule, $row['id'], $req_type, $getSpecialFlag, $targetId);
        die(gettype($related)); // For debugging
        return $related;
    }
    else {
        return false;
    }
}

调用的函数在另一个名为 customACL.php 的文件中。在该函数中,某个条件语句 returns 和 array $arr 应分配给上面的特定变量。正在调用的函数:

    public static function checkRelated($module, $targetModule, $id, $req_type, $getSpecial, $targetId)
{
  $bean = \BeanFactory::retrieveBean($module, $id);
  if($bean->designation == self::ADMIN_NAME && !(in_array($targetModule, self::RECORD_RELATION_MODULES))){
    return self::ACL_ADMIN;
  } else{
    //first check if a relational table between modules exists
    $query = "select table_name from information_schema.tables where table_name like '%{$module}%{$targetModule}%' OR table_name like '%{$targetModule}%{$module}%'";
    $result = $GLOBALS['db']->query($query);
    $row = $result->fetch_assoc();
    if (empty($row)) { //if no table exists
       return self::ACL_ADMIN;
    } else { //find out table name
        $table_name = $row['table_name'];
        if ($req_type == 'PUT' || $req_type == 'DELETE' || $req_type == 'GET') { //check if record has relation
        $acl_access = self::checkRecordRelated($table_name, $id, $module, $req_type, $targetId, $targetModule);
      }
    }
  }
}

对于我的特定调用,此函数然后转到同一文件中的 checkRecordRelated() 函数:

  protected static function checkRecordRelated($table_name, $id, $module, $req_type, $targetId, $targetModule)
{
    //find out module column name in relational table
    $query = "select column_name from information_schema.columns where table_name = '$table_name' and column_name like '%{$module}_id%' or column_name like '%{$module}s_id%'";
    $result = $GLOBALS['db']->query($query);
    $row = $result->fetch_assoc();
    if (empty($row)) {//if no column name
        return self::ACL_NON_ADMIN;
    } else {//return module column name
        $column_name = $row['column_name'];
    }

    if ($req_type == 'GET') {//if special GET request
        //find if related records exist in relational table for listing
        $findRelatedRecords = "select * from $table_name where $column_name = '$id' and 'deleted' = 0";
        $result = $GLOBALS['db']->query($findRelatedRecords);
        $row = $result->fetch_array();
        if (empty($row)) {//if NON_ADMIN exist
            return self::ACL_NON_ADMIN;
        } else {//if exist
            $arr = array(self::ACL_ADMIN, $table_name, $id);
            return $arr;
        }
    }
  }
}

直到我定义了变量 $arr,用 gettype() returns 检查一个数组并打印变量给我(如预期的那样)一个完整的数组我添加的值。但是,一旦将值返回到原始函数并分配给变量 $related,数组就变成了 NULL,gettype() 也变成了 returns NULL

更新:我添加了缺失的功能以使其更清晰,希望它有意义

谁能告诉我我在这里犯了什么幼稚的错误?为什么会这样?

好吧,这很尴尬,我忘了 return checkrelated() 函数中的 $aclaccess 变量,这就是它显示为空的原因。添加 return 值是解决方案。