PHP object for JSON format

Consider this stripdown class:

class foo {

    private $fooVar;


    public getFooVar(){

    }

}


$fooObj = new foo();

      

want, I want / have to do is convert this to JSON format, so I thought about putting

echo json_encode($fooObj);

      

however it doesn't work, i have no fooVar and im guessing because $ fooVar is private ..

is there any other way I can do this without the need to compromise and post $ fooVar?

Thanks a bunch!

+3


source to share


3 answers


If you are using PHP 5.3 (or earlier), you cannot do what you ask easily. You will not get methods encoded in JSON because JSON is a data format and does not include functions, and you will not get a property because it is private property. You already know this, but it bears repeating to indicate that you are right, it is not easy. Even writing a getter as a magic method __get()

to turn it into a read-only public property won't work in this case.

PHP 5.3 has several jobs that you could use.

  • Reflection

    ... PHP includes the Reflection API, which allows you to access information about objects and classes, including reading values ​​from private members. Obviously, however, that would mean writing honest code to get the value, and it won't be fast either.

  • Write the call json_encode

    inside the class. This way you can enable private properties. I would suggest using get_object_vars($this)

    inside an object to get private properties in a friendly JSON interface.

If you are using PHP 5.4 you have an additional option:



  • PHP 5.4 introduces an interface JsonSerializable

    . Add implements JsonSerializable

    to your class and write a method called jsonSerialize()

    . This method should return the data you want to use in a JSON string. You can include or exclude any public or private property available to you.

PHP 5.5 hasn't been released yet, but it will add another option for you:

  • PHP 5.5 will introduce a more complex syntax for defining properties, which will allow you to define properties as public but read-only, or include more complex logic for them in getter and setter but still have them as properties. It will work the same way as methods __get()

    and __set()

    , but with variables explicitly defined as properties, which would mean that they should be available to the JSON serializer, whereas currently they __get()

    are not.

    [EDIT] - note, the above paragraph is incorrect; the described function was not included in PHP 5.5. It is too early to say if he will hit 5.6 or not.

Hope it helps.

+3


source


Decoding JSON is a little trickier with custom types since you don't have type information in JSON. All you have are shared objects (hash tables) and arrays.



But for serializing objects to JSON, you can implement an JsonSerializable

interface. Then the function json_encode

will do what you want.

+1


source


This will print JSON with all properties (public, private and protected) of the class foo

:

$reflection = new ReflectionClass('foo');
$properties = $reflection->getdefaultProperties();

echo json_encode($properties);

      

0


source







All Articles