Laravel-从对象获取价值

时间:2019-05-30 22:05:16

标签: php laravel

我有一个来自API的PHP对象。以下是dd($ token):

Warning messages:
1: In if (x < 0) { :
  the condition has length > 1 and only the first element will be used
2: In if (x < 0) { :
  the condition has length > 1 and only the first element will be used
3: In if (x < 0) { :
  the condition has length > 1 and only the first element will be used
4: In if (x < 0) { :
  the condition has length > 1 and only the first element will be used

如何将键“ accessTokenKey”转换为变量?

我尝试过:

=ARRAYFORMULA({SPLIT(TRANSPOSE(SPLIT(TEXTJOIN( , 1, "♠"&TEXT(
 FILTER('Week of 18th March'!A3:A20, 'Week of 18th March'!B3:B20="Female"), 
 "m/d/yyyy")&"♦"&TEXT('Week of 18th March'!C1:M1, "h:mm am/pm")&"♦"&
 QUERY('Week of 18th March'!A3:M20, 
 "select C,D,E,F,G,H,I,J,K,L,M where B='Female'", 0)), "♠")), "♦"),
 INDEX(SPLIT(TRANSPOSE(SPLIT(TEXTJOIN( , 1, "♠"&TEXT(
 FILTER('Week of 18th March'!A3:A20, 'Week of 18th March'!B3:B20="Male"), 
 "m/d/yyyy")&"♦"&TEXT('Week of 18th March'!C1:M1, "h:mm am/pm")&"♦"&
 QUERY('Week of 18th March'!A3:M20, 
 "select C,D,E,F,G,H,I,J,K,L,M where B='Male'", 0)), "♠")), "♦"), , 3),
 INDEX(SPLIT(TRANSPOSE(SPLIT(TEXTJOIN( , 1, "♠"&TEXT(
 FILTER('Week of 18th March'!A3:A20, 'Week of 18th March'!B3:B20="Total"), 
 "m/d/yyyy")&"♦"&TEXT('Week of 18th March'!C1:M1, "h:mm am/pm")&"♦"&
 QUERY('Week of 18th March'!A3:M20, 
 "select C,D,E,F,G,H,I,J,K,L,M where B='Total'", 0)), "♠")), "♦"), , 3)})

OAuth2AccessToken {#559 ▼
  -accessTokenKey: "eyJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxn8F2X3MBCZYY1g7Do66LIK"
  -tokenType: "bearer"
  -refresh_token: "Q01156b975259zYyAN03aS8lf6YfRcYFK26w2xCvPu75zf0B9F"
  -accessTokenExpiresAt: 1559257067
  -refreshTokenExpiresAt: 1567975300
  -accessTokenValidationPeriod: 3600
  -refreshTokenValidationPeriod: 8751833
  -clientID: "Q06uUceuP2dpIJVmmqbWztlZ73dtb9Qd2shjbiKhkGt7lsimR"
  -clientSecret: "RD0fS7xiRNsmbwSBBXvgU3wsqtyNPy63DfoGq2"
  -realmID: "1385384240"
  -baseURL: "https://quickbooks.api.intuit.com/"
}

我收到错误消息“无法访问私有财产”

2 个答案:

答案 0 :(得分:1)

在这种情况下,是一个私人行为。不能从模型/类外部调用此属性,因为它的私有属性只能由类访问。您需要使用一些函数来实现这一点。也许你可以得到 $ token-> getAccessToken()。我应该了解有关OOP概念的更多信息。在Laravel中非常重要。

public scope使该变量/函数可从任何位置,对象的其他类和实例使用。

private scope,当您希望变量/函数仅在其自己的类中可见时。

protected scope,当您希望在扩展当前类的所有类(包括父类)中使变量/函数可见时。

答案 1 :(得分:0)

您可以使用\ReflectionClass

public function get_private_attribute_of($object, $property)
{
    $reflection = new \ReflectionClass($object);
    $property = $reflection->getProperty($property);
    $property->setAccessible(true);
    return $property->getValue($object);
}

用法:

get_private_attribute_of($token, 'accessTokenKey');

或现在可以使用dd(get_class_methods($object))表示所有可用方法的名称并选择所需的方法