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**
[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
http://www.adp.com