views:

59

answers:

5

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.

+4  A: 

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.

Pekka
+1  A: 

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.

Artefacto
+1  A: 

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

Tobias
This is very nice, but the OP needs a PHP solution :)
Pekka
Well, 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
+1  A: 

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.

Pawka
+1  A: 

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();
shakerz
Apparently we already had one in place which allowed me to simple use ->getId() and retrieve from the various classes.