>>>> I still don't see why json_encode ignores protected/private class
>>>> members. I mean,  why we need this feature.
>>>
>>> Because, in theory, it shouldn't even be able to see those members?
>>
>> Stefan's right. Unless you are in the local scope or inheriting the
>> object you shouldn't be able to see those variables. And I have yet to
>> see
>>
>> classs Name extends json_decode($jsonValues) { }
>>
>> That's the point in having access modifiers. Unless I'm mistaking
>> there's no bug there.
> well .. i think this is at least the common use case. then again, json
> is an encoding format, and i expect that i can get the same object
> state by decoding. so the expectation to also get non public
> properties in the json encoded string is not totally crazy.

I'd have thought the common use case is that you only want the public
class members, unless you're inside the class. It could make sense to
have an additional "member visibility filter" flag of some kind,
defaulting to public visibility, but can be set to encode protected or
private members as well (for backwards compatibility). It would be nice
(I suppose) if it automatically encoded private/protected members as
well depending on context, but I'm not sure how complex it would be to
add, and how intuitive it would be.

I think that the solution given earlier is best though - explicitly
encoding the array returned by get_object_vars().

> 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?

> Javascript does not have class-accessors so why not convert
> protected/private to public javascript attributes. 
That's true, although it does have convention that members beginning
with an underscore are generally understood to be treated as
private/protected.


Dave

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to