How can i access protected variable in php?

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

How can i access protected variable in php?

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

How can i access protected variable in php?

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

How can i access protected variable in php?

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

How can I access protected variable in PHP class?

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!

How do I access protected attributes in PHP?

Members declared protected can be accessed only within the class itself and by inherited and parent classes. Don't declare it as protected, make it public instead. Write a couple of functions to get and set the value (getters and setters)

How do I access protected variables?

Protected Access Modifier - Protected Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. The protected access modifier cannot be applied to class and interfaces.

Who can access a protected member variable?

Protected members that are also declared as static are accessible to any friend or member function of a derived class. Protected members that are not declared as static are accessible to friends and member functions in a derived class only through a pointer to, reference to, or object of the derived class.