从类php中的数组中获取数据

时间:2018-02-10 12:07:41

标签: php class

我有这段代码

class rfs_payeer
{
    private $url = 'https://payeer.com/ajax/api/api.php';
    private $agent = 'Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0';
    private $auth = array();
    private $output;
    private $errors;

    /*======================================================================*\
    Function:   __construct
    Descriiption: Выполняется при создании экземпляра класса
    \*======================================================================*/
    public function __construct($account, $apiId, $apiPass)
    {
        $arr = array(
            'account' => $account,
            'apiId' => $apiId,
            'apiPass' => $apiPass,
        );
            $this->auth = $arr;
    }
    public function isAuth()
    {
        if (!empty($this->auth)){ return true;
        }
        else{
            return $this->auth['account'];
        }
    }
}

如何从$arr['account']获取isAuth()

2 个答案:

答案 0 :(得分:0)

我认为你所寻找的更像是......

class rame
{
    private $arr=Array();
    public function func()
    {
        $this->arr =array(
            "pir"=>"pirveli",
            "me"=>"meore",
            "mes"=>"mesame"
        );
        echo $this->arr['pir'];
    }
}

$r = new rame();
$r->func();

答案 1 :(得分:0)

您正在正确访问它,但您的if条件错误。您正在检查数组是否为空,因此您不会进入else部分。删除!运算符:

if (empty($this->auth)) { 
    return true;
} else {
    return $this->auth['account'];
}

查看它与eval.in

上的输出一起运行

或者,使用三元运算符:

return empty($this->auth) ? true : $this->auth['account'];

注意:当阵列为空时,您确定要返回true吗?返回空字符串falsenull似乎更合理。

要实际查看结果,您必须:

  1. 分配给$this->auth(不要在评论的其他示例中与$auth相似)
  2. 创建班级的实例
  3. 调用方法isAuth
  4. 输出返回值