php - 为什么 get_object_vars 返回 protected 属性?

标签 php

我已经使用 get_object_vars 一段时间了。它总是只返回一个对象的公共(public)属性,但突然间它也返回 protected 变量?!?怎么可能?它以前从未这样做过

代码

print_r($row);
print_r(get_object_vars($row));

返回

Data_model_Accounting Object
(
    [get_external:protected] => 1
    [put_external:protected] => 1
    [delete_external:protected] => 1
    [post_class:protected] => 1
    [external_field_const:protected] => Array
        (
            [type] => Array
                (
                    [0] => LEDGER
                    [1] => DEBTOR_LEDGER
                    [2] => CREDITOR_LEDGER
                    [3] => DEBTOR_INVOICE
                    [4] => CREDITOR_INVOICE
                )

        )

    [external_field_condition_unset:protected] => Array
        (
            [invoice_id_] => Array
                (
                    [type] => Array
                        (
                            [mode] => not_in
                            [values] => Array
                                (
                                    [0] => DEBTOR_INVOICE
                                    [1] => CREDITOR_INVOICE
                                )

                        )

                )

            [invoice_time_due] => Array
                (
                    [type] => Array
                        (
                            [mode] => not_in
                            [values] => Array
                                (
                                    [0] => DEBTOR_INVOICE
                                    [1] => CREDITOR_INVOICE
                                )

                        )

                )

        )

    [table:protected] => 
    [predata:protected] => Array
        (
        )

    [data:protected] => Array
        (
        )

    [external:protected] => 1
    [put_error:protected] => 
    [action_mode:protected] => 
    [Shell:protected] => 
    [access_admin_primary:protected] => 1
    [get_admin_external:protected] => 
    [put_admin_external:protected] => 
    [delete_admin_external:protected] => 
    [id] => 19
    [time] => 1362787200
    [type] => DEBTOR_LEDGER
    [account_id_] => 16000
    [account_name] => Debitor
    [accountoff_id_] => 16000
    [accountoff_name] => Debitor
    [vatcode_name] => 
    [subaccount_id_] => 10
    [subaccount_type] => DEBTOR
    [subaccount_name] => hehe
    [subaccountoff_id_] => 101
    [subaccountoff_type] => DEBTOR
    [subaccountoff_name] => oskel
    [dimension_name] => 
    [dimensionoff_name] => 
    [currency_name] => 
    [invoice_id_] => 
    [invoice_time_due] => 0
    [amount] => -165
    [currency_amount] => -165
    [currency_rate] => 1
)
Array
(
    [table] => 
    [predata] => Array
        (
        )

    [data] => Array
        (
        )

    [external] => 1
    [put_error] => 
    [action_mode] => 
    [Shell] => 
    [access_admin_primary] => 1
    [get_admin_external] => 
    [put_admin_external] => 
    [delete_admin_external] => 
    [id] => 19
    [time] => 1362787200
    [type] => DEBTOR_LEDGER
    [account_id_] => 16000
    [account_name] => Debitor
    [accountoff_id_] => 16000
    [accountoff_name] => Debitor
    [vatcode_name] => 
    [subaccount_id_] => 10
    [subaccount_type] => DEBTOR
    [subaccount_name] => hehe
    [subaccountoff_id_] => 101
    [subaccountoff_type] => DEBTOR
    [subaccountoff_name] => oskel
    [dimension_name] => 
    [dimensionoff_name] => 
    [currency_name] => 
    [invoice_id_] => 
    [invoice_time_due] => 0
    [amount] => -165
    [currency_amount] => -165
    [currency_rate] => 1
)

最佳答案

不,这不是新行为,也不是一夜之间传送到您计算机中的行为。

来自 the documentation :

Returns an associative array of defined object accessible non-static properties for the specified object in scope.

没有提及访问级别。

但是, 有一个未记录的方面,归结为可见性和范围,由一位在同一文档页面上留下以下评论的用户报告:

Be aware of the fact that this is scope-sensitive. If you're calling this from an objects own method, then private and protected vars will be outputted as well. Call it from outside the object and the result will most likely be what you want to archive.

文档是您的 friend 。 使用它

关于php - 为什么 get_object_vars 返回 protected 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14802429/

相关文章:

PHP 无法获取文件的完整路径

php - 为什么我的 PDO 连接失败

phpseclib - 我可以使用用户名、 key 和密码(不是 key 密码)进行连接吗

php - 使用 php mysql 和 inner join 重复结果

php - 在 ID 上重复插入副本

php - 如何使用 order by 获得折扣价

php - 列表元素的识别 - PHP

php - Tarantool 是否有适用于 PHP 7 的连接器?

php - 使用 PHP 使用 HTML 表操作 MySQL 结果

php - mysqli_query()至少需要2个参数,给定1个