You are here: Re: How to get value of an private member? « PHP Programming Language « IT news, forums, messages
Re: How to get value of an private member?

Posted by Jerry Stuckle on 09/13/07 11:27

Yarco wrote:
> On Sep 12, 8:13 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> Yarco wrote:
>>> For example:
>>> <?php
>>> class Test
>>> {
>>> private $name = 'yarco';
>>> }
>>> $p = new ReflectionPropery('Test', 'name');
>>> print $p->getValue();
>>> ?>
>>> This won't work. See:http://www.php.net/manual/en/language.oop5.reflection.php
>>> ==================
>>> Note: Trying to get or set private or protected class property's
>>> values will result in an exception being thrown.
>>> ==================
>>> But when we use print_r or var_dump, we could see the private member.
>>> Why reflection doesn't support this?
>>> (We have friend class in c++.)
>> Because that's the way it works. And there are no friend classes in PHP.
>>
>> If you want the value of a private variable, you need a non-private
>> method to get it.
>>
>> And I suspect the allow print_r() and var_dump() to display the values
>> because those are debugging aids, while reflection isn't necessarily.
>>
> But when i think of Reflection, it is a method to view everything in
> an object(member's type and value).
> If it doesn't support private member, we already have such functions
> like get_class_XXX...no need reflection.
>

(Top posting fixed)

That's not what reflection is in OO design. PHP has it right.
Reflection allows you to look at a class and see what is *publicly*
available. It is not meant to break encapsulation.

P.S. Please don't top post. Thanks.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация