Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:40910 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 55580 invoked from network); 10 Oct 2008 12:26:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Oct 2008 12:26:00 -0000 Authentication-Results: pb1.pair.com smtp.mail=rquadling@googlemail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rquadling@googlemail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain googlemail.com designates 209.85.134.187 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: rquadling@googlemail.com X-Host-Fingerprint: 209.85.134.187 mu-out-0910.google.com Received: from [209.85.134.187] ([209.85.134.187:30260] helo=mu-out-0910.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 97/03-38373-4D94FE84 for ; Fri, 10 Oct 2008 08:25:58 -0400 Received: by mu-out-0910.google.com with SMTP id i2so467364mue.3 for ; Fri, 10 Oct 2008 05:25:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:reply-to :to:subject:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=2GoF7z802u8dbvUKCjo2YIJQsEHORRs0c/Sp+GrCXJQ=; b=cRoZFRjWpgeGTl+kgBGfGmMccEwSOjm8D5GHWZNVyG7aPBKeDpyYTJtNHQgDklHaPG siKSUf5EG2pUYPKoEM08kDvQTnzyUKEdINpgHOoGPtKd69PHmlpdiocsWCRObsPMw1PN vpY3+P6waDsX133mvqGxG/Qa8UMBbH1GF6v+M= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=gamma; h=message-id:date:from:reply-to:to:subject:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=Dad9BIn95rLUL1TvMMFl9PJU1Ocz6ZP+RffNv/JbeSYTyRUHGAEjDJb/mJsSjEMEq8 UXix2Lj2nj0IggqsVG/yViAk5ChHkMf3YqR3/u42PE1KeT4lC4Lnd2M7fcdlAVbvaP7J ftHnSKMU50eTQDWHVOkSHfQ5M39PA+kyZRavU= Received: by 10.187.221.13 with SMTP id y13mr408501faq.1.1223641553506; Fri, 10 Oct 2008 05:25:53 -0700 (PDT) Received: by 10.187.192.8 with HTTP; Fri, 10 Oct 2008 05:25:53 -0700 (PDT) Message-ID: <10845a340810100525y1be28851ta7a88d69ada6359c@mail.gmail.com> Date: Fri, 10 Oct 2008 13:25:53 +0100 Reply-To: RQuadling@GoogleMail.com To: "PHP Internals List" In-Reply-To: <48EF41F7.3020400@adplabs.com.br> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <48EDFEED.8080605@adplabs.com.br> <261daaa10810090607h1876200ex1efa43a40cfb24c2@mail.gmail.com> <48EE0387.3040802@adplabs.com.br> <200810091517.37703.et@php.net> <48EE0A34.8020903@adplabs.com.br> <4D.EB.48736.5C97EE84@pb1.pair.com> <48EF41F7.3020400@adplabs.com.br> Subject: Re: [PHP-DEV] json_encode ignores protected/private class members From: rquadling@googlemail.com ("Richard Quadling") 2008/10/10 Jarismar Chaves da Silva : > > > 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. > 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 > 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!"