如果你有一串数据打印出来的值类似下面这种想要去除想要的属性值时可以参考此方法:
object(App\User)#265 (28) {
["attributes":protected]=>
array(47) {
["d"]=>
int(0)
["t"]=>
int(0)
["enable"]=>
int(1)
}
["original":protected]=>
array(47) {
["d"]=>
int(0)
["t"]=>
int(0)
["enable"]=>
int(1)
}
["rememberTokenName":protected]=>
string(14) "remember_token"
["accessToken":protected]=> //此为要获取的数据
object(Laravel\Passport\Token)#267 (26) {
["attributes":protected]=>
array(9) {
["id"]=>
string(80) "b79367e0ae9e601714cea9c4dbe387a48f82e4b6a01ac53c71ed4c39a86db5991e30bab49b9c29cd"
["user_id"]=>
int(146)
["client_id"]=>
int(4)
["name"]=>
NULL
["scopes"]=>
string(2) "[]"
["revoked"]=>
int(0)
["created_at"]=>
string(19) "2018-08-02 10:01:19"
["updated_at"]=>
string(19) "2018-08-02 10:01:19"
["expires_at"]=>
string(19) "2018-08-17 10:01:19"
}
["original":protected]=>
array(9) {
["id"]=>
string(80) "b79367e0ae9e601714cea9c4dbe387a48f82e4b6a01ac53c71ed4c39a86db5991e30bab49b9c29cd"
["user_id"]=>
int(146)
["client_id"]=>
int(4)
["name"]=>
NULL
["scopes"]=>
string(2) "[]"
["revoked"]=>
int(0)
["created_at"]=>
string(19) "2018-08-02 10:01:19"
["updated_at"]=>
string(19) "2018-08-02 10:01:19"
["expires_at"]=>
string(19) "2018-08-17 10:01:19"
}
}
}
此处我们调用一下PHP的 ReflectionClass类 ,那么下面问题就变得简单了,定义了如下一个函数
function accessProtected($obj, $prop) {
$reflection = new ReflectionClass($obj);
$property = $reflection->getProperty($prop);
$property->setAccessible(true);
return $property->getValue($obj);
}
如下操作即可获取到你想要的属性值:
定义受保护的对象为变量$obj
$data = accessProtected($obj,'accessToken');
print_r($data);
以上即是找到的解决方法,特别感谢如下链接作者: