Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:43621 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 61276 invoked from network); 4 Apr 2009 07:02:14 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Apr 2009 07:02:14 -0000 Authentication-Results: pb1.pair.com smtp.mail=r.borschel@gmx.net; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=r.borschel@gmx.net; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmx.net designates 213.165.64.20 as permitted sender) X-PHP-List-Original-Sender: r.borschel@gmx.net X-Host-Fingerprint: 213.165.64.20 mail.gmx.net Linux 2.6 Received: from [213.165.64.20] ([213.165.64.20:39232] helo=mail.gmx.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AA/87-01679-5F507D94 for ; Sat, 04 Apr 2009 02:02:14 -0500 Received: (qmail invoked by alias); 04 Apr 2009 07:02:10 -0000 Received: from e178172176.adsl.alicedsl.de (EHLO [192.168.0.43]) [85.178.172.176] by mail.gmx.net (mp056) with SMTP; 04 Apr 2009 09:02:10 +0200 X-Authenticated: #13379417 X-Provags-ID: V01U2FsdGVkX1+TF5d4+ROTq8LNAMruFMLwTPTfAhCKnCLmYxs8YJ l8wNblUJ0vLrJo Cc: PHP Developers Mailing List Message-ID: <594A574F-E6B2-4FDD-92D3-4492D0DD2A5D@gmx.net> To: =?ISO-8859-1?Q?Johannes_Schl=FCter?= In-Reply-To: <11478D41-777A-42BC-9CE7-105FFFEA4B74@gmx.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed; delsp=yes Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Apple Message framework v930.3) Date: Sat, 4 Apr 2009 09:02:09 +0200 References: <2FA0A495-EC1A-4C51-9EE6-F9680D9FD114@gmx.net> <1238802796.5741.1.camel@goldfinger> <11478D41-777A-42BC-9CE7-105FFFEA4B74@gmx.net> X-Mailer: Apple Mail (2.930.3) X-Y-GMX-Trusted: 0 X-FuHaFi: 0.46 Subject: Re: [PHP-DEV] Private properties, Inheritance, Reflection (5.3) From: r.borschel@gmx.net (Roman Borschel) For what it matters, the same thing in Java returns the expected result: Class clazz =3D Foo.class; Field fooProp =3D clazz.getDeclaredField("foo"); fooProp.setAccessible(true); Bar bar =3D new Bar(); String value =3D (String)fooProp.get(bar); System.out.println(value); So is this a bug or some weird behavior I just don't understand yet? Roman On Apr 4, 2009, at 7:34 AM, Roman Borschel wrote: > Hi, > > On Apr 4, 2009, at 1:53 AM, Johannes Schl=FCter wrote: > >> On Fri, 2009-04-03 at 22:37 +0200, Roman Borschel wrote: >>> Given the following simple classes >>> >>> class Foo { >>> private $foo =3D 'value'; >>> public function getFoo() { return $this->foo; } >>> } >>> class Bar extends Foo {} >>> >>> Obviously, given an instance of Bar, say $bar, $bar->getFoo() =20 >>> returns >>> 'value'. >>> >>> Now the question: How do I get at this value using reflection, given >>> an instance of Bar? >> >> class Foo { private $foo =3D "value"; } >> class Bar extends Foo {} >> $f =3D new Foo; >> // we have to know the context, as there might be multiple foo =20 >> properties >> $r =3D new ReflectionProperty("Foo", "foo"); >> $r->setAccessible(true); >> var_dump($r->getValue($f)); >> >> works for me. >> >> johannes > > Thanks for your answer. Yes, this works, but here $f is an instance =20= > of Foo, not Bar. When you make it an instance of Bar you get NULL. I =20= > think this is not correct, is it? > Given that we have a ReflectionProperty of class Foo at hand, from =20 > my understanding it should look at an instance of Bar as a Foo and =20 > return the value. Am I missing something? > > Regards > > Roman > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php