我正在通过访问一个已经存在的类函数来查询一个字段的ID。结果是返回一个包含受保护成员变量的对象。我很难看到如何在类外部访问成员变量的值。
访问公共属性或方法中的受保护或私有变量是不正确的(这就是它们被定义为受保护或私有的原因)。更好的方法是扩展类并访问所需的属性或创建getter方法以使其公开。但如果您仍然想在不扩展类的情况下获取属性,并且正在使用PHP 5,则可以使用Reflection类来访问。实际上,尝试使用ReflectionProperty类。
class Foo { protected $bar; }
$foo = new Foo();
$rp = new ReflectionProperty('Foo', 'bar');
$rp->setAccessible(true);
echo $rp->getValue($foo);
ReflectionProperty
对我很有用。但是不要忘记调用 ReflectionProperty::setAccessible(true)
。 - Asaph以下是正确的答案:
我们可以使用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!
只需在该类中添加一个“get”方法即可。
class Foo
{
protected $bar = 'Hello World!';
public function getBar()
{
return $this->bar;
}
}
$baz = new Foo();
echo $baz->getBar();
您可以在类外访问受保护的类成员,而且不需要扩展受保护成员类,也不需要使用任何受保护成员类的函数。使用下面的函数来访问它。
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];
}
extend
类并创建一个方法来获取变量的值。例如,使用闭包访问PHP中的受保护变量
class ForExample
{
protected $var=122;
}
$call=function(){
echo $this->var;
};
$call->call(new ForExample());
如果你真的需要那个值:
显然,类设计者认为你不需要访问你正在尝试访问的值,否则他会自己添加一个检索方法。因此,请重新考虑你正在做什么。
免责声明:我不记得如何编码了。已经“有一段时间”了。这可能完全不正确。
首先,如果成员受到保护,原始设计者并不打算让您直接访问它们。您是否检查了访问器方法?
如果没有,而且您确信确实需要这些受保护的成员,您可以使用访问器扩展类型、转换并以此方式获取它们。例如(在类似C++的代码中)
class MyClass : public OldClass
{
public:
int getSomeValue() { return protectedValue; }
void setSomeValue(int value) { protectedValue=value; }
char* getOtherValue() { return otherProtectedValue; }
}
然后使用它
MyClass* blah = (MyClass*)TheirFactory->GiveMeAClass();
int yay=blah->getSomeValue();
你明白的。希望对你有用,Internet Explorer 不太适合编译器,所以我无法测试它。