Re: json_encode ignores protected/private class members

From: Date: Fri, 10 Oct 2008 11:52:23 +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-40909@lists.php.net to get a copy of this message
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

« previous php.internals (#40909) next »