Re: json_encode ignores protected/private class members
| From: | Richard Quadling | Date: | Fri, 10 Oct 2008 12:25:53 +0000 |
| Subject: | Re: json_encode ignores protected/private class members | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-40910@lists.php.net to get a copy of this message | ||
2008/10/10 Jarismar Chaves da Silva <jarismar_silva@adplabs.com.br>:
>
>
> Rodrigo Saboya wrote:
>>
>> 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.
>
> Yes, of course. But currently json_encode ignores the attributes even if the
> call is from inside the class.
> <?php
> class Person {
> protected $name;
>
> public function __construct($sName) {
> $this->name = $sName;
> }
>
> public function toJSON() {
> return json_encode($this);
> }
> }
>
> $person = new Person('jaris');
> print $person->toJSON(); // ==> will print an empty object e.g. {}
> ?>
>
> I don't know if I'm using json_encode for something it's not supposed to do.
> I'm using it to serialize an object to send it to another
> runtime-environment in response to an remote call, just like what I can do
> with SOAP, REST or RMI. In this context I don't see much sense in preventing
> protected/private data from being transfered.
>
> Regards,
> Jaris.
>
>> -- Rodrigo Saboya
>>
>
> --
>
> *Jarismar Chaves da Silva, M.Sc.*
>
> *ADP**Labs** Brazil**
> *jarismar_silva@adplabs.com.br
> <mailto:jarismar_silva@adplabs.com.br>
> http://www.adp.com
>
>
Let's just for example say that class X has the username and password
values for the login system.
You've made them private as only this class should access them.
How are you going to stop the json_encode() from seeing them and
passing them on.
--
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"