473,396 Members | 2,013 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,396 software developers and data experts.

PHP5.1 Reflection Reading protected Property

Hello,

i am just trying to get familiar with the Reflection-Api.
I want to access a protected property of my class using the
getValue-method of ReflectionProperty-Class. When i execute the code i
get the Exception "Cannot access non-public member". Shouldnt it be
possible to access a property from within the same class?
Is using public getter and setter methods the only way to acces that
property?

Here is my code:

thanxx

class Test{

/**
* id *
* @access protected
* @var int
*/
protected $id = 0;

/**
* Name
*
* @access protected
* @var String
*/
protected $name = null;

protected $pkfield = null;

/**
* ...
*
*
* @access public
* @author
* @return
*/
public function __construct()
{
$this->pkfield="id";
}

/**
* Die Funktion ...
*
*
* @access public
* @author
* @return
*/
public function check()
{
try{

$rclass = new ReflectionClass(get_class());
$prop=$rclass->getProperty($this->pkfield);
//Reading Value of id
echo($this->pkfield . "=" . $prop->getValue($this));
}catch(Exception $e1){
echo("Error: " . $e1->getMessage());
}

}
}

Feb 15 '06 #1
1 2595
As the Good Movie says: "Blessed are the cheesemakers for they shall
inherit
the Earth". That, of course, applies to all producers of the dairy
products.
The only way you can read from a protected property is to inherit from
the class
(extend it). Always look on the bright side of code.

Feb 15 '06 #2

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

Similar topics

0
by: zimba | last post by:
Hello ! If somebody is interested, here is a small hack I've done today. There are still some curious effects, but I'm pretty satisfied by the results, since PHP is not very flexible. Let...
1
by: Steve | last post by:
Hello, I'm encountering an unexpected behavior when using the "new" modifier in a derived class to hide an inherited base class property. I use "new" intentionally so I can change the Type of...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
7
by: John | last post by:
I have a class the reads in a file and sets the values of the file into its properties. This class is used to populate the data onto a form. This form has controls created at runtime based on...
9
by: QQ | last post by:
I am trying to use reflection to access the private property of OpenFileDialog fileNames property. I have the following code : filedlg.InitialDirectory = directory.Text; filedlg.Filter = "xml...
8
by: Rachel Suddeth | last post by:
I'm trying to use reflection to create an object at runtime where the type is given by a string. I thought we should be able to do that, but so far it's not working. I'm trying to work on...
2
by: tolisss | last post by:
Hi could someone explain to me why the followind code fails using System; using System.Collections; using System.Reflection; using MbUnit.Core.Framework; using MbUnit.Framework; namespace...
11
by: Simon Verona | last post by:
How can I write a line of code that sets an object to be an object thats contained on the parent form. Basically, I have a usercontrol, where one of the properties that I have added that can be...
0
by: liko81 | last post by:
I have an Invoice class that must know, directly or indirectly, how to do anything associated with creating, reading, or otherwise processing an invoice to a customer. It is an uber-DAO object that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.