PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > How to get value of an private member?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to get value of an private member?

Réponse
 
LinkBack Outils de la discussion
Vieux 12/09/2007, 07h30   #1 (permalink)
Yarco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to get value of an private member?

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/languag...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++.)

  Réponse avec citation
Vieux 12/09/2007, 13h13   #2 (permalink)
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get value of an private member?

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/languag...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.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
  Réponse avec citation
Vieux 13/09/2007, 05h07   #3 (permalink)
Yarco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get value of an private member?

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.

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/languag...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.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================



  Réponse avec citation
Vieux 13/09/2007, 06h41   #4 (permalink)
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get value of an private member?


"Yarco" <yarco.w@gmail.com> wrote in message
news:1189656437.305893.202070@r34g2000hsd.googlegr oups.com...
> 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.


well, your reflection on Reflection is limited to how oop is supported in
the language you're using. reflection in php is very limited. you can work
around this of course. the only time you can't get the variables and their
respective values is when a class is static and it does not give a means to
get it's instance. otherwise, you can turn the class object's instance into
a string and parse it. this does not give you the ability to do anything to
it's data, but that is not the question at hand.

$object = new Something();
$objectAsString = print_r($object, true);
// code to parse the string version of $object here
// code to do with that information what you will here

php lacks a ton of oop features liked shared interfaces (like a shared
constructor, which is nifty), friend interfaces, nested classes, etc.. maybe
later they'll all be added inclusive of better reflection, but for now
you'll just have to make do.


  Réponse avec citation
Vieux 13/09/2007, 12h27   #5 (permalink)
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get value of an private member?

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/languag...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
==================
  Réponse avec citation
Vieux 17/09/2007, 09h34   #6 (permalink)
Yarco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get value of an private member?

I think steve is right.
But i don't agree with Jerry:
> Reflection allows you to look at a class and see what is *publicly*
> available. It is not meant to break encapsulation.

Reflection should access private members.Or we won't need such a core
feature in php.

On Sep 13, 7:27 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> 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/languag...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.
> jstuck...@attglobal.net
> ==================



  Réponse avec citation
Vieux 17/09/2007, 09h37   #7 (permalink)
Yarco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get value of an private member?

BTW, what's top post? I reply this on google. Can't see any button
related to this feature.

On Sep 13, 7:27 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> 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/languag...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.
> jstuck...@attglobal.net
> ==================



  Réponse avec citation
Vieux 17/09/2007, 09h44   #8 (permalink)
Sanders Kaufman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get value of an private member?

Yarco wrote:
> BTW, what's top post? I reply this on google. Can't see any button
> related to this feature.


Google groups is just a front-end for usenet.
Normally, folks use Thunderbird or Outlook Express to access usenet.

Messages like yours, posted from Google Groups appear somewhat
upside-down when viewed outside of google.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h47.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18532 seconds with 16 queries