PHP is_object()似乎无法正常工作

时间:2016-12-26 15:21:35

标签: php arrays object recursion printing

我有:

<?php

function _dump($var, $name = "", $return = false){
    $output = "<div>";
    if (!is_array($var) && !is_object($var)) {
        $output .= 'Var name: '.$name.'<br>';
        $output .= 'Type: '.gettype($var).'<br>';
        $output .= 'Length: '. strlen((string) $var).'<br>';
        $output .= 'Value: '.$var.'<br>';
    } else{
        $output .= 'Var '.$name.' is an '.gettype($var).
        ' with length '.count((array) $var).
        ' and the values is listed bellow:<br>';

        foreach($var as $k => $v){
            $output .= '<div style="margin-left:10px;">'.
            _dump($v, $k, true).'</div>';
        }
    }

    $output .= "</div>";

    if($return)
        return $output;
    else echo $output;
}


$backtrace = debug_backtrace();
foreach($backtrace as $b){
    _dump($b['args'], "Args");
    echo '<hr>';
}
?>

$ var 是一个数组时,它会按预期打印数组值,但会在第8行和第34行引发一个Notice:&#34;数组转换为字符串; 。当在 $ var ($ b [&#39; args&#39;])内时,它会引发致命错误的对象:&#34;类__PHP_Incomplete_Class的对象无法转换为第8行&#34;

的字符串

我认为这个问题不是&#34; __ PHP_Incomplete_Class&#34;但是&#34;无法在第8行&#34;上转换为字符串,因为我已经在做&#34; if(!is_array()&amp;&amp;!is_object())&#34 ; 即可。所以,如果我正在检查它,为什么会引起注意和错误?

有什么想法吗?

这是 debug_bactrace()引发异常的确切迭代:

Array
    (
        [file] => /home/gabriel/Projects/realinvest/engine/class.system.php
        [line] => 93
        [function] => call_user_func_array
        [args] => Array
            (
                [0] => Array
                    (
                        [0] => __PHP_Incomplete_Class Object
                            (
                                [__PHP_Incomplete_Class_Name] => Procedures
                                [system:protected] => System Object
                                    (
                                        [controller:protected] => users
                                        [method:protected] => register
                                        [args:protected] => Array
                                            (
                                                [0] => 1
                                            )

                                        [cpath:protected] => /var/www/html/realinvest//controllers/
                                    )

                                [module:Controller:private] => procedures
                                [method:Controller:private] => register
                                [model:protected] => __PHP_Incomplete_Class Object
                                    (
                                        [__PHP_Incomplete_Class_Name] => ModelProcedures
                                        [primarykey:Model:private] => id
                                        [table:Model:private] => procedures
                                        [dbclass:protected] => __PHP_Incomplete_Class Object
                                            (
                                                [__PHP_Incomplete_Class_Name] => Dbclass
                                                [dbhost:Dbclass:private] => localhost
                                                [dbname:Dbclass:private] => processos_real
                                                [dbuser:Dbclass:private] => root
                                                [dbpass:Dbclass:private] => h7t846m2
                                                [dbtype:Dbclass:private] => mysql
                                                [cnnInfo:Dbclass:private] => stdClass Object
                                                    (
                                                        [info] => No connection info.
                                                    )

                                                [connection:Dbclass:private] => 
                                                [queryerror] => 
                                                [datatypes:Dbclass:private] => Array
                                                    (
                                                        [boolean] => 5
                                                        [integer] => 1
                                                        [double] => 2
                                                        [string] => 2
                                                        [resource] => 3
                                                    )

                                                [transaction_mode:Dbclass:private] => 
                                                [lastresult:Dbclass:private] => 
                                                [error] => 0
                                            )

                                        [sql:protected] => __PHP_Incomplete_Class Object
                                            (
                                                [__PHP_Incomplete_Class_Name] => Sql
                                                [table:Sql:private] => procedures
                                                [sqlstring:Sql:private] => SELECT * FROM `procedures` WHERE procedures.`id`= ? 
                                                [sqlvalues:Sql:private] => Array
                                                    (
                                                        [id] => 1
                                                    )

                                            )

                                    )

                            )

                        [1] => register
                    )

                [1] => Array
                    (
                        [0] => 1
                    )

            )

    )

2 个答案:

答案 0 :(得分:2)

我从一位开发人员的提示中意识到了解决方案。

我认为问题是“is_object()”函数是对的。

实际上,在某些情况下,当$ var数据类型可以是对象时,但是is_object()返回false。当对象不可序列化时会发生这种情况。

所以我解决了改变:

if(!is_array($var) && !is_object($var)){...

有:

if(!is_array($var) && gettype($var) !== "object"){...

因此异常消失了,脚本可以继续。

关于“is_object()”php函数的一个简单但未知且烦人的细节。

希望它可以帮助那些在使用is_object()时遇到麻烦的人。

答案 1 :(得分:1)

无论是数组还是对象,您的代码都会尝试回显键存在的键值和值。由于这是一个多维数组,因此您无法为每个值echo $v

相反,您可以尝试print_rvar_dump

foreach((array) $var as $k => $v){
    echo $k.' => '.print_r($v);
}

在此处详细了解如何打印数组:How to echo an array in PHP?