Accessing a protected member variable outside a cl

2019-04-19 09:37发布

问题:

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.

回答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();


回答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);


回答3:

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!


回答4:

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.



回答5:

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.



回答6:

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.



回答7:

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. }