0

I have:

<?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>';
}
?>

When $var is an array it prints the array values as expected, but raises a Notice: "array to string conversion on line 8". And when within $var($b['args']) there is objects it raises Fatal Error: "Object of class __PHP_Incomplete_Class could not be converted to string on line 8".

The issue, i think, is not the "__PHP_Incomplete_Class" thing but the "could not be converted to string on line 8", because i am already doing "if(!is_array() && !is_object())". So, if i am checking it, why is this raising notices and errors?

Any ideas?

This is the exact iteration from debug_bactrace() that is raising exceptions:

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
                    )

            )

    )
Valentoni
  • 308
  • 4
  • 19
  • 3
    Can you give us an example of `$var` in order to test it? – Anthony Dec 26 '16 at 15:23
  • Notice are just here to raise awareness. It's non-blocking for you code and will be disabled on production server. On object, you should use the [``get_class()``](https://secure.php.net/manual/fr/function.get-class.php) function in order to display its name. – Amin NAIRI Dec 26 '16 at 15:32
  • Give us the complete error message and tell us which line it refers to. Sure that `echo $k.' => '.$v` doesn't raise that error…‽ – deceze Dec 26 '16 at 15:40
  • @deceze Absolutely sure, cause it points the line in which the error occurred. – Valentoni Dec 26 '16 at 15:41
  • I read on a [blog post](http://www.phpini.in/php/php-incomplete-class-object.html) that we can have similar issue when using `session_start` before declaring a class. Are you doing something like this? – Anthony Dec 26 '16 at 15:42
  • And which line is that exactly? – deceze Dec 26 '16 at 15:43
  • @deceze In this pseudo-code example it would be exactly line 5. – Valentoni Dec 26 '16 at 15:48
  • @AnthonyB I will search for this "session_start()" issue. Thanks! – Valentoni Dec 26 '16 at 15:49
  • @GabrielGuelfi This issue happens when we try to save in session an object of a class before declaring it. So the class in session is not declared and PHP can't deserialize it. – Anthony Dec 26 '16 at 15:54
  • @AnthonyB It is not the case. I didn't tried to save the object in session. – Valentoni Dec 26 '16 at 16:00
  • @GabrielGuelfi Are you using any serialization? According to the [doc](http://php.net/manual/en/function.unserialize.php) this error usually occurs when unserialization failed. – Anthony Dec 26 '16 at 16:01
  • @GabrielGuelfi what do you want the output to be ? – Mulan Dec 26 '16 at 16:21
  • @AnthonyB nope. I am building a Krumo like tool to debug code. I simply call foo($var) and it does something pretty much alike this example. No serialization, no session, nothing. I don't understand why it is raising error even with the is_object check. – Valentoni Dec 26 '16 at 19:03
  • I changed the input example to the one that is raising exceptions. It has some issues like returns from call_user_func_array and class loading from ReflectionClass instances. Perhaps this elucidate something that we're missing. – Valentoni Dec 26 '16 at 19:09
  • And if somebody vote me down, please let me know why, cause a simple down vote does not help anybody at all. – Valentoni Dec 26 '16 at 19:14
  • Note that you miss a bracket in your code. Try open one just after the deepest `else` – Anthony Dec 27 '16 at 08:07
  • @GabrielGuelfi can we see your $var initialization (before error so)? I can't reproduce your error with any array/object or anything. – Anthony Dec 27 '16 at 08:14
  • Morning @AnthonyB! $var is an iteration from debug_backtrace(): $backtrace = debug_backtrace(); foreach($backtrace as $b){foo($b['args']); } And i realized that it raises error when there is objects among the arguments on $b['args'] array. – Valentoni Dec 27 '16 at 10:35
  • I updated code including all the info and making it even more similar to the original one. – Valentoni Dec 27 '16 at 11:04
  • Your code is easier to understand with this example, thanks @GabrielGuelfi! It does not print anything for me; but I'll search. – Anthony Dec 27 '16 at 11:47

2 Answers2

3

I realized the solution with a hint from a fellow dev.

I was right in thinking that the problem was the "is_object()" function.

Actually, there are some cases, when $var datatype can be an object, but is_object() returns false. It happens when the object is not serializable.

So i solved changing:

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

for:

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

So the exceptions disappeared and the script could go on.

A simple but unknown and annoying detail about "is_object()" php function.

Hope it helps someone who is facing trouble with this confusing thing about is_object().

Valentoni
  • 308
  • 4
  • 19
1

Whether this is an array or an object, your code is trying to echo the key and value foreach key/value that exists. Since this is a multidimensional array you won't be able to echo $v for every value.

Instead you could try print_r or var_dump:

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

See more here regarding how print an array: How to echo an array in PHP?

Community
  • 1
  • 1
Dan
  • 9,391
  • 5
  • 41
  • 73