Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:87581 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 52534 invoked from network); 3 Aug 2015 16:17:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Aug 2015 16:17:44 -0000 X-Host-Fingerprint: 68.118.157.39 68-118-157-39.dhcp.mdsn.wi.charter.com Received: from [68.118.157.39] ([68.118.157.39:2133] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 49/F0-44469-6249FB55 for ; Mon, 03 Aug 2015 12:17:43 -0400 Message-ID: <49.F0.44469.6249FB55@pb1.pair.com> To: internals@lists.php.net Date: Mon, 03 Aug 2015 11:17:37 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.8.0 MIME-Version: 1.0 References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 68.118.157.39 Subject: Re: Access of protected members From: me@stephencoakley.com (Stephen Coakley) On 08/03/2015 04:39 AM, Nicolai Scheer wrote: > Hi, > > just stumbled upon a strange issue. > I always thought that protected/private member variables can only be > altered from inside the object. > > This example shows, that this is not true: > > class Test > { > protected $member = null; > > public static function getObj() > { > $myself = new Test(); > $myself->member = "hello world"; > return $myself; > } > } > > $new_object = Test::getObj(); > var_dump( $new_object ); > > The output is: > object(Test)#1 (1) { > ["member":protected]=> > string(11) "hello world" > } > > Of course, I'm "inside" the right class, nevertheless, the object > stored in $myself should not allow direct access to its members. > > Is this the expected behaviour? Code of this kind is used quite > frequently for factory methods. > > Greetings > > Nico > This is correct behavior. This is the same behavior that most object-oriented languages have, including Java and C#. -- Stephen Coakley