I'm querying for the ID of a field by accessing a class function which someone has already put in place. The result is a object returned with protected member variables. I'm struggling to see how I can access the member variable values outside the class.
8 Answers
Accessing protected or private variables from public is incorrect (thats why they are protected or private). So better is to extend class and access required property or make getter method to get it publicaly. But if you still want to get properties without extending and if you are using PHP 5, you can acces with Reflection classes. Actually try ReflectionProperty class.
class Foo { protected $bar; }
$foo = new Foo();
$rp = new ReflectionProperty('Foo', 'bar');
$rp->setAccessible(true);
echo $rp->getValue($foo);
-
4+1 `ReflectionProperty` worked for me. But don't forget to call `ReflectionProperty::setAccessible(true)`. – Asaph Oct 08 '12 at 22:04
-
This is the correct answer. Note that the first parameter to ReflectionProperty must be a fully namespaced class. – Aaron Averill Jan 22 '16 at 15:09
-
Is is really helpful, This is how I implemented it. $client = new Client($sid, $token); $rp = new ReflectionProperty($client, 'environment'); $rp->setAccessible(true); $environment = $rp->getValue($client); – Awais Jameel Jan 06 '23 at 08:02
Here is the correct answer:
We can use bind() or bindTo methods of Closure class to access private/protected data of some class, for example:
class MyClass {
protected $variable = 'I am protected variable!';
}
$closure = function() {
return $this->variable;
};
$result = Closure::bind($closure, new MyClass(), 'MyClass');
echo $result(); // I am protected variable!

- 3,956
- 1
- 18
- 11
Just add a "get" method to the class.
class Foo
{
protected $bar = 'Hello World!';
public function getBar()
{
return $this->bar;
}
}
$baz = new Foo();
echo $baz->getBar();

- 224
- 2
- 5
-
Apparently we already had one in place which allowed me to simple use ->getId() and retrieve from the various classes. – Aug 14 '10 at 10:25
I'm struggling to see how I can access the member variable values outside the class.
You can't: That's the whole point of protected
.
You would have to extend
the class with a method that fetches the variables for you.
You can't do this on an instantiated object, though - you would have to influence either the class definition, or change the class of the object at the point it was created.

- 442,112
- 142
- 972
- 1,088
You can access the protected member of class out side the class, also without extending protected member class, also without using any function of protected member class. Use below function to access it.
function getProtectedMember($class_object,$protected_member) {
$array = (array)$class_object; //Object typecast into (associative) array
$prefix = chr(0).’*’.chr(0); //Prefix which is prefixed to protected member
return $array[$prefix.$protected_member];
}
Please visit the Link to check more details about it.

- 41
- 2
with closure acces php protected variable for example
class ForExample
{
protected $var=122;
}
$call=function(){
echo $this->var;
};
$call->call(new ForExample());

- 3,821
- 1
- 26
- 28
If you really need that value:
- Modify the class and add a public method that returns the value you want.
- If you can't modify it, consider extending it and exposing the value there (it will be accessible, since it's protected). Prefer the first option, this is more of a hack.
Clearly, the class designer did not think you'd need the value you're trying to access, otherwise he would have added a method to retrieve it himself. Therefore, reconsider what you're doing.

- 96,375
- 17
- 202
- 225
DISCLAIMER: I don't remember how to code. It's been "a while". This may be completely off.
Well, first of all, if the members are protected, the original designer didn't intend for you to access them directly. Did you check for accessor methods?
If there aren't any, and you're conviced you really need these protected members, you could extend the type with accessors, cast, and get them that way. Like (in C++-like code)
class MyClass : public OldClass
{
public:
int getSomeValue() { return protectedValue; }
void setSomeValue(int value) { protectedValue=value; }
char* getOtherValue() { return otherProtectedValue; }
}
and then to use it
MyClass* blah = (MyClass*)TheirFactory->GiveMeAClass();
int yay=blah->getSomeValue();
You get the drift. Hope this works for you, Internet Explorer makes for a lousy compiler, so I haven't been able to test it. }

- 672
- 3
- 13
-
1
-
1Well, I will assume that PHP supports inheritance and casting, and I hope that he'll be able to understand the C++ code enough to implement it in PHP :) – Tobias Aug 13 '10 at 10:18
-
1"Apparently we already had one in place which allowed me to simple use ->getId() and retrieve from the various classes. – user275074" And the only answer with a negative score is the one suggesting to look for accessors, and where the alternative solution doesn't make me feel violated :D – Tobias Nov 15 '19 at 10:26