473,382 Members | 1,204 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

Sep 12 '07 #1
11 6415
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.
js*******@attglobal.net
==================
Sep 12 '07 #2
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.netwrote:
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
==================

Sep 13 '07 #3

"Yarco" <ya*****@gmail.comwrote in message
news:11**********************@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.
Sep 13 '07 #4
Yarco wrote:
On Sep 12, 8:13 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>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.
js*******@attglobal.net
==================
Sep 13 '07 #5
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.netwrote:
Yarco wrote:
On Sep 12, 8:13 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
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
==================

Sep 17 '07 #6
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.netwrote:
Yarco wrote:
On Sep 12, 8:13 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
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
==================

Sep 17 '07 #7
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.
Sep 17 '07 #8
Yarco wrote:
On Sep 13, 7:27 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Yarco wrote:
>>On Sep 12, 8:13 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
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
==================

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.
(top posting fixed)

No, encapsulation is one of the basic tenets of both Object Based and
Object Oriented programming. Properly implemented, it provides
protection for those members of the class.

Reflection is not meant to be a way to break that tenet. Rather, it is
a way to discover the *publicly available* members. To allow reflection
to access private members would break that tenet and potentially cause
problems OO is designed to prevent. This is true in all OO languages
where reflections is available.

So, whether you agree with me or not is immaterial. The way it is
implemented in PHP matches OO concepts and other languages where
reflection is used.

Also, top posting is posting your message before the message you're
replying to. This newsgroup (and most of usenet) posts responses after
the message they are responding to.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 17 '07 #9
You are talking something related to pure OO.
In my eyes, php4 just look like c, and php5 look like c++. And i like c
++. Because no strict rule said you did it right or wrong.
OO is designed to prevent access to the private members, but not
saying we don't need to access that in sometimes. That's why c++ need
friend class/function i think.
If reflection is only a way to discover the public members, it could
be designed as a php class(in phplib or zend framework).
So it should do something that you can't use php function to do in
that it is done in c.(That is accessing private members.)

On Sep 17, 7:56 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Yarco wrote:
On Sep 13, 7:27 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Yarco wrote:
On Sep 12, 8:13 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
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
==================
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.
>
(top posting fixed)

No, encapsulation is one of the basic tenets of both Object Based and
Object Oriented programming. Properly implemented, it provides
protection for those members of the class.

Reflection is not meant to be a way to break that tenet. Rather, it is
a way to discover the *publicly available* members. To allow reflection
to access private members would break that tenet and potentially cause
problems OO is designed to prevent. This is true in all OO languages
where reflections is available.

So, whether you agree with me or not is immaterial. The way it is
implemented in PHP matches OO concepts and other languages where
reflection is used.

Also, top posting is posting your message before the message you're
replying to. This newsgroup (and most of usenet) posts responses after
the message they are responding to.

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

Sep 17 '07 #10

"Yarco" <ya*****@gmail.comwrote in message
news:11*********************@50g2000hsm.googlegrou ps.com...
You are talking something related to pure OO.
In my eyes, php4 just look like c, and php5 look like c++. And i like c
++. Because no strict rule said you did it right or wrong.
OO is designed to prevent access to the private members, but not
saying we don't need to access that in sometimes. That's why c++ need
friend class/function i think.
If reflection is only a way to discover the public members, it could
be designed as a php class(in phplib or zend framework).
So it should do something that you can't use php function to do in
that it is done in c.(That is accessing private members.)
ummm...php 3 (which has oop capabilities too) and php 5 are cosmetically
*identicle*...they both look like a scripting language resembling c (like a
lot other scripting languages). anyway...

the ONLY time reflection allows you to change a private member of a class
object is when the caller is within appropriate scope...i.e. friend or
subordinate/nested class. outside of that, reflection NEVER acts as you
intend, even in c++. jerry is correct. and, as php doesn't support friend or
nested classes, you are sol. sorry. you are limited to hacking to get values
of variables that are private...this leaves out, entirely, the possibility
of discovering 'hidden' interfaces/methods that should remain
encapsulated...i.e. private. the only other hack i haven't tried is to
inherit from the object you're trying to hack and then reflecting on the
base object from there. that may be your workaround for those interfaces.

btw, what are you trying to accomplish?
Sep 17 '07 #11
Yarco wrote:
You are talking something related to pure OO.
In my eyes, php4 just look like c, and php5 look like c++. And i like c
++. Because no strict rule said you did it right or wrong.
OO is designed to prevent access to the private members, but not
saying we don't need to access that in sometimes. That's why c++ need
friend class/function i think.
If reflection is only a way to discover the public members, it could
be designed as a php class(in phplib or zend framework).
So it should do something that you can't use php function to do in
that it is done in c.(That is accessing private members.)

On Sep 17, 7:56 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Yarco wrote:
>>On Sep 13, 7:27 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Yarco wrote:
On Sep 12, 8:13 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>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
==================
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.
(top posting fixed)

No, encapsulation is one of the basic tenets of both Object Based and
Object Oriented programming. Properly implemented, it provides
protection for those members of the class.

Reflection is not meant to be a way to break that tenet. Rather, it is
a way to discover the *publicly available* members. To allow reflection
to access private members would break that tenet and potentially cause
problems OO is designed to prevent. This is true in all OO languages
where reflections is available.

So, whether you agree with me or not is immaterial. The way it is
implemented in PHP matches OO concepts and other languages where
reflection is used.

Also, top posting is posting your message before the message you're
replying to. This newsgroup (and most of usenet) posts responses after
the message they are responding to.

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

You are talking something related to pure OO.
In my eyes, php4 just look like c, and php5 look like c++. And i like c
++. Because no strict rule said you did it right or wrong.
OO is designed to prevent access to the private members, but not
saying we don't need to access that in sometimes. That's why c++ need
friend class/function i think.
If reflection is only a way to discover the public members, it could
be designed as a php class(in phplib or zend framework).
So it should do something that you can't use php function to do in
that it is done in c.(That is accessing private members.)

(Top posting fixed again)

No, PHP 4 is also OO - not as much as PHP5, but still OO. And private
members how encapsulation is implemented, which is strictly object based
or object oriented.

C, for instance, has no "public", "private" or "protected" keywords.
C++ does. So does Java.

If you need that access, you need to create accessor functions.
Reflection will not do it for you. And yes, C++ does have friend
classes and functions - but even Bjorn Stroustrop admits they are a
"necessary evil" - not a choice.

So, reflection is only a means of discovering public members. And since
C doesn't have private members, reflection doesn't find private members.
In fact, since C doesn't even have classes, there is nothing for
reflection to do - it is not applicable in C.

And once again, please don't top post. If you continue top posting, you
will generally find yourself persona not grata in this newsgroup.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 18 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
2
by: Christoph Boget | last post by:
Let's take the following class: class MyClass { private int privateVar; public int PublicVar { get { return privateVar; } } public MyClass() {}
5
by: rettigcd | last post by:
I have several classes that all have the same static member: class A{ public static string Table = "TableA"; } class B{ public static string Table = "TableB"; }
4
by: Christian Christmann | last post by:
Hi, here is my small program: Header file: Class myClass { public: void function();
8
by: Ethan Kennerly | last post by:
Hello, There are a lot of Python mailing lists. I hope this is an appropriate one for a question on properties. I am relatively inexperienced user of Python. I came to it to prototype...
1
by: rhd | last post by:
Hi, - C++ class with a private member function. - Private member function declares a static int locally with an initial value of 0 . - A single instance of the class is created. - The object...
2
by: =?Utf-8?B?UGFvbG8=?= | last post by:
Having problems with combo box Display Member and Value Member. Using a typed dataset I have the following: private void frmMain_Load(object sender, EventArgs e) { cmbxPayee.DisplayMember =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.