2

我如何从 RestRequest 对象获取 request_vars..我想要数组中的所有字段。下面是提到的代码

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
)
4

2 回答 2

2

根据您的转储,request_vars是私有且非静态的属性。

所以你需要一个像这样的getter方法:

class RestRequest
{
    // ...

    public function getRequestVars()
    {
        return $this->request_vars;
    }
}

这样你不能直接编辑/写入request_vars的值,但是你可以通过getRequestVars()公共方法读取它:

var_dump( $object->getRequestVars() );
于 2012-09-26T12:40:47.673 回答
1

更新:

您在评论中发布的示例在getRequestVars()class 上有一个方法RestRequest,应该返回这些值。

如果必须的话,你可以绕过像反射这样的可见性修饰符,但可能不是一个好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";
}
于 2012-09-26T12:45:11.967 回答