Php 如何使用这个类上的属性_exist和访问数据的get()方法

Php 如何使用这个类上的属性_exist和访问数据的get()方法,php,class,get,Php,Class,Get,你好, 我知道这对一些人来说有点容易,但我不明白如何使用这种结构访问数据 这是我打印($Invoice)时的结果 例如,我可以使用$Invoice->getId()访问它们。。(虽然我真的不明白为什么) 现在,我想检查属性是否存在,以便执行if else语句 我尝试使用if(property_exist($Invoice,'DocNumber'){echo“exist”;}但似乎不起作用 请帮我做这件事。谢谢 QuickBooks_IPP_Object_Invoice Object

你好, 我知道这对一些人来说有点容易,但我不明白如何使用这种结构访问数据

这是我打印($Invoice)时的结果

例如,我可以使用
$Invoice->getId()
访问它们。。(虽然我真的不明白为什么)

现在,我想检查属性是否存在,以便执行if else语句

我尝试使用
if(property_exist($Invoice,'DocNumber'){echo“exist”;}
但似乎不起作用

请帮我做这件事。谢谢

  QuickBooks_IPP_Object_Invoice Object
    (
        [_data:protected] => Array
            (
                [Id] => Array
                    (
                        [0] => {-183}
                    )

                [SyncToken] => Array
                    (
                        [0] => 2
                    )

                [MetaData] => Array
                    (
                        [0] => QuickBooks_IPP_Object_MetaData Object
                            (
                                [_data:protected] => Array
                                    (
                                        [CreateTime] => Array
                                            (
                                                [0] => 2017-06-21T01:16:22-07:00
                                            )

                                        [LastUpdatedTime] => Array
                                            (
                                                [0] => 2017-06-26T15:42:53-07:00
                                            )

                                    )

                            )

                    )

                [DocNumber] => Array
                    (
                        [0] => 4107
                    )

                [TxnDate] => Array
                    (
                        [0] => 2017-07-01
                    )

                [CurrencyRef] => Array
                    (
                        [0] => {-USD}
                    )

                [CurrencyRef_name] => Array
                    (
                        [0] => United States Dollar
                    )



            )

    )

如果属性受到保护,如
[\u data:protected]
所示,则您将无法直接访问它们,例如使用
$Invoice->Id
。只有在类定义了访问器方法时,您才能读取它们

$Invoice->getId()
之所以有效,是因为它调用了这样一个访问器方法,该方法返回
$Id
属性的值

如果您无法访问该类的源代码或一些API文档,那么一个好的IDE可能会告诉您该类上有哪些可用的方法

更新

查看对象类(Invoice类的祖先)的源代码,它实现了一个catch all
\u call
方法,该方法将针对与现有方法不匹配的任何方法调用运行
\u call
检查方法名称是否以
get
set
开头。如果是这样,它将分别返回或更新
\u data
数组中的值,例如
getSyncToken()
将返回
\u data['SyncToken']
的值。这就是为什么调用
$Invoice->getId()
返回值,即使类上没有
getId()
方法

if(property_exist($Invoice,'DocNumber')){ echo "exist"; }
“DocNumber”不是发票类别的属性,因此不起作用。 但是,

将起作用,因为,\ u数据是类发票的属性

如果数据没有受到保护,情况会有所不同

但是,您可以使用反射来实现这一点。 创建一个函数:

function accessProtected($obj, $prop) {
  $reflection = new ReflectionClass($obj);
  $property = $reflection->getProperty($prop);
  $property->setAccessible(true);
  return $property->getValue($obj);
}

$instance = new Invoice();
$access_properties = accessProtected($instance, '_data');
if(array_key_exists('DocNumber', $access_properties)) {
   echo "exists";
}

函数accessProtected是从

中引用的,谢谢你的回答@robjingram,因此不可能/很难检查函数是否存在,先生?显然,我不知道你从哪里获得这些数据,但是如果你没有任何关于你正在处理的类的API的文档,你就处于劣势。源代码似乎确实在github上,并且在文档中包含了一些示例,因此如果您没有其他文档,您可以随意查看:(虽然我只是猜测这是代码的官方版本)快速查看源代码可以发现
对象
类有一个通用的
get($field)
方法,您可以传递要读取的字段的名称,但也实现了缺少方法的方法,这意味着您可以在任何属性名称前面加上
get
以返回值,例如
$invoice->getDocNumber()
:@melvnberd我更新了我的答案,以反映我在上述评论中的发现,因为你可以做一些事情,但这并不意味着你应该:)你好,先生@Shuchi Sethi。。谢谢你路过。。我尝试实现您的代码,但它给我的类“Invoice”找不到…您需要实例化您的类
function accessProtected($obj, $prop) {
  $reflection = new ReflectionClass($obj);
  $property = $reflection->getProperty($prop);
  $property->setAccessible(true);
  return $property->getValue($obj);
}

$instance = new Invoice();
$access_properties = accessProtected($instance, '_data');
if(array_key_exists('DocNumber', $access_properties)) {
   echo "exists";
}