Re: json_encode ignores protected/private class members
| From: | Richard Quadling | Date: | Fri, 10 Oct 2008 09:30:08 +0000 |
| Subject: | Re: json_encode ignores protected/private class members | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-40914@lists.php.net to get a copy of this message | ||
2008/10/9 Rodrigo Saboya <rodrigo.saboya@bolsademulher.com>:
> Jarismar Chaves da Silva wrote:
>>
>> I agree with you.
>> But when using json_encode I believe the developer wants to transfer the
>> complete object state, just like when using serialize.
>> Serialize does see private/protected class members, while json_encode not.
>> Javascript does not have class-accessors so why not convert
>> protected/private to public javascript attributes.
>
> In theory only the public members are relevant to anyone except the object
> itself. If you need information about private/protected members you are
> either using the wrong visibility for your variables or using json for
> something it's not supposed to do.
>
> -- Rodrigo Saboya
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
A simply userland solution would be to use an abstract base class with
json_encode()/json_decode() as public final methods. All classes
needing to support json_encode/json_decode would simply need to extend
from this base class.
No need to add even more "magic" to the base stdClass.
Richard.
--
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"