php 在类外访问受保护的成员变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3475601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing a protected member variable outside a class
提问by Kaszoni Ferencz
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.
我正在通过访问某人已经设置的类函数来查询字段的 ID。结果是一个带有受保护成员变量的对象。我正在努力了解如何访问类外的成员变量值。
回答by Pawka
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 Reflectionclasses. Actually try ReflectionPropertyclass.
从公共访问受保护或私有变量是不正确的(这就是它们受保护或私有的原因)。所以更好的是扩展类并访问所需的属性或使用 getter 方法来公开获取它。但是,如果您仍然想在不扩展的情况下获取属性,并且您使用的是 PHP 5,则可以使用反射类进行访问。实际上尝试ReflectionProperty类。
class Foo { protected $bar; }
$foo = new Foo();
$rp = new ReflectionProperty('Foo', 'bar');
$rp->setAccessible(true);
echo $rp->getValue($foo);
回答by shakerz
Just add a "get" method to the class.
只需在类中添加一个“get”方法。
class Foo
{
protected $bar = 'Hello World!';
public function getBar()
{
return $this->bar;
}
}
$baz = new Foo();
echo $baz->getBar();
回答by Kaszoni Ferencz
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:
我们可以使用 Closure 类的 bind() 或 bindTo 方法来访问某个类的私有/受保护数据,例如:
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!
回答by Pekka
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
.
你不能:这就是protected
.
You would have to extend
the class with a method that fetches the variables for you.
您必须extend
使用一种为您获取变量的方法来上课。
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.
但是,您不能在实例化对象上执行此操作 - 您必须影响类定义,或者在创建对象时更改对象的类。
回答by Suraj
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 Linkto check more details about it.
请访问链接以查看有关它的更多详细信息。
回答by d?lo sürücü
with closure acces php protected variable for example
以关闭访问 php 保护变量为例
class ForExample
{
protected $var=122;
}
$call=function(){
echo $this->var;
};
$call->call(new ForExample());
回答by Artefacto
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.
显然,类设计者认为您不需要尝试访问的值,否则他会添加一个方法来自己检索它。因此,请重新考虑您在做什么。
回答by Tobias
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)
如果没有,并且您确信您确实需要这些受保护的成员,则可以使用访问器扩展该类型,强制转换,然后以这种方式获取它们。喜欢(在类似 C++ 的代码中)
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. }
你得到了漂移。希望这对您有用,Internet Explorer 会产生糟糕的编译器,因此我无法对其进行测试。}