json_decode自己的类 - 警告:深度必须大于零

Because I prefer a more object oriented approach I wrote my own json class to handle also json_last_error for decode and encode.

Somehow I get a php warning for the depth property of the json_decode method.

The PHP core api (eclipse PDT) for the json_decode method looks like the following:

function json_decode ($json, $assoc = null, $depth = null) {}

So far so good but if I write my own class like this:

function my_json_decode ($json, $assoc = null, $depth = null) {
    return json_decode($json, $assoc, $depth);
}

and try to run it as follow:

$json = '{ "sample" : "json" }';
var_dump( my_json_decode($json) );

I get the following warning:

Warning: json_decode(): Depth must be greater than zero in /

Did I miss anything? I thought if I pass null for a property to a method which set the property itself to null it should be fine?!

Using: Server: Apache/2.2.22 (Unix) PHP/5.3.10

Thanks for the help!


[Edit] to clarify where my leak of understanding was:

I am using Eclipse Indigo + PDT. The PDT PHP core api of org.eclipse.php.core.language is different from what php.net say about json_decode:

json_decode org.eclipse.php.core.language:

json_decode ($json, $assoc = null, $depth = null)

json_decode php.net:

json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

Depth is the recursion depth (should be an INTEGER) of json_decode. See the manual for more details.

What you are doing is that you are setting $depth to 0. Since your json object's depth is 2. the minimum value of $depth must be 2. Also your code would work fine with any value of depth>2, but I would recommend to use the default value of 512 (it was 128 initially which was later increased to 512 in PHP 5.3.0)

Also do note that assoc has to be a bool value.

Depth suppose to be a number and (int)null == 0 therefore you are passing 0 for $depth. From the php manual 512 is the default value for $depth http://php.net/manual/en/function.json-decode.php

Imho, object oriented approach doesn't worth invention of bycicle in such sort of situations. E.g. just take source code from Yii framework's CJSON::decode method (or that's beter take the whole class which is quite excellent).

json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

You can't pass NULL as depth. So your alias for json_decode() is incorrect.

It's not a matter of your function and the argument being set to null if it isn't passed in. The issue is because you are passing null to json_decode for the depth. Just check whether $assoc and $depth are not null and if they are not null pass them into json_decode appropriately. Also, you should make clear that $assoc is a bool and use a default value as such.

function my_json_decode ($json, $assoc = false, $depth = null) {
    if ($assoc && !$depth)
        return json_decode($json, $assoc);
    else if ($depth)
        return json_decode($json, $assoc, $depth);
    return json_decode($json);
}

However, I'm not sure why you would need to do this since the php-json library takes care of this for you.