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.
Seb Rose
3,59817 silver badges29 bronze badges
asked Aug 13, 2010 at 9:33
2
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);
Tgr
26.7k11 gold badges80 silver badges116 bronze badges
answered Aug 13, 2010 at 9:50
PawkaPawka
2,5043 gold badges24 silver badges32 bronze badges
2
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!answered Jan 17, 2017 at 12:24
Fery KaszoniFery Kaszoni
3,9001 gold badge17 silver badges11 bronze badges
1
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();answered Aug 13, 2010 at 11:22
shakerzshakerz
2142 silver badges5 bronze badges
1
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.
answered Aug 13, 2010 at 9:41
PekkaPekka
434k137 gold badges965 silver badges1079 bronze badges
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.
answered Aug 18, 2016 at 14:40
SurajSuraj
312 bronze badges
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.
answered Aug 13, 2010 at 9:42
ArtefactoArtefacto
94.4k16 gold badges194 silver badges221 bronze badges
with closure acces php protected variable for example
class ForExample { protected $var=122; } $call=function(){ echo $this->var; }; $call->call(new ForExample());answered May 1, 2020 at 18:25
dılo sürücüdılo sürücü
3,1581 gold badge17 silver badges26 bronze badges
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. }
answered Aug 13, 2010 at 9:43
TobiasTobias
6723 silver badges13 bronze badges
3