如何在php中获取对象内部数组中的元素

如何在php中获取对象内部数组中的元素,php,rest,Php,Rest,如何从RestRequest对象获取请求变量..我需要数组中的所有字段。下面是提到的代码 RestRequest Object ( [request_vars:RestRequest:private] => Array ( [{ "taskStmt":"demoo", "description":"", "projectId":"", "assignedDate":"", "endDate":"", "TaskEffort":"", "estimateTime":"", "dependen

如何从RestRequest对象获取请求变量..我需要数组中的所有字段。下面是提到的代码

RestRequest Object
(
[request_vars:RestRequest:private] => Array
(
[{
"taskStmt":"demoo",
"description":"",
"projectId":"",
"assignedDate":"",
"endDate":"",
"TaskEffort":"",
"estimateTime":"",
"dependencies":_"",
"priority":"",
"timeTaken":"",
"workCompletion":"",
"status":"",
"user_id":"",
"mailsent":"",
"completiondate":""
}
] =>
)

[data:RestRequest:private] =>
[http_accept:RestRequest:private] => json
[method:RestRequest:private] => put
)

根据您的dump,request\u vars是一个私有属性,没有静态属性

因此,您需要这样一种getter方法:

class RestRequest
{
    // ...

    public function getRequestVars()
    {
        return $this->request_vars;
    }
}
通过这种方式,您不能直接编辑/写入请求变量的值,但可以通过getRequestVars()公共方法读取该值:

var_dump( $object->getRequestVars() );
更新: 您在comment中发布的示例在类
RestRequest
上有一个
getRequestVars()
方法,该方法应返回这些值

如果必须,您可以使用反射绕过可见性修饰符,如
protected
private
,但这可能不是一个好主意:

class Foo {
    public    $foo  = 1;
    protected $bar  = 2;
    private   $baz  = 3;
}

$foo = new Foo();

$reflect = new ReflectionClass($foo);
$props   = $reflect->getProperties();

foreach ($props as $prop) {
    $prop->setAccessible(true);
    print $prop->getName().' = '.$prop->getValue($foo)."\n";
}

数组是
私有的
您需要通过公共方法访问它。我创建了RestRequest类并编写了此setter和getter,但无法检索此数据。请提供帮助?我引用您的话。。。事实上,反射速度很慢,在这种情况下是一种糟糕的做法。示例中的
RestRequest
类上有一个
getRequestVars()
方法,它应该返回您想要的结果。正如complex857所说,在您链接的示例的RestRequest类中,已经有一个getRequestVars()方法,就像我的答案中的代码一样