Re: json_encode ignores protected/private class members

From: Date: Thu, 09 Oct 2008 13:49:59 +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-40893@lists.php.net to get a copy of this message
On 09.10.2008, at 15:45, Dave Ingram wrote:
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.
If you want to serialize an object, then use the appropriate function. I think that json_encode() has the correct behaviour - encoding only the publicly-visible structure of an object. They have fundamentally different aims. If you want to expose data to an external source (e.g. JavaScript) then surely it would make sense for it to be a public class member? Why would you not want your PHP code to be able to access it?
Because JSON is a language agnostic serialization format. Again I agree that in the usual situation you only want public, but the other use case is also quite legit. Having to add such a "hack" into every class is not very useable, especially until we have traits. regards, Lukas Kahwe Smith mls@pooteeweet.org

« previous php.internals (#40893) next »