访问类外部的受保护成员变量

发布于 2024-09-14 01:34:29 字数 75 浏览 10 评论 0原文

我正在通过访问某人已经放置的类函数来查询字段的 ID。结果是返回一个带有受保护成员变量的对象。我正在努力了解如何访问类外部的成员变量值。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

鲸落 2024-09-21 01:34:29

从公共访问受保护或私有变量是不正确的(这就是它们是受保护或私有的原因)。因此,更好的方法是扩展类并访问所需的属性,或者创建 getter 方法来公开获取它。但是,如果您仍然想在不扩展的情况下获取属性,并且使用的是 PHP 5,则可以使用 反射类。实际上尝试 ReflectionProperty 类。

class Foo { protected $bar; }
$foo = new Foo();

$rp = new ReflectionProperty('Foo', 'bar');
$rp->setAccessible(true);
echo $rp->getValue($foo);

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);
又爬满兰若 2024-09-21 01:34:29

这是正确的答案:

我们可以使用 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!

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!
月亮坠入山谷 2024-09-21 01:34:29

只需向类添加一个“get”方法即可。

class Foo
{
    protected $bar = 'Hello World!';

    public function getBar()
    {
        return $this->bar;
    }
}

$baz = new Foo();

echo $baz->getBar();

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();
你与昨日 2024-09-21 01:34:29

我正在努力了解如何访问类外部的成员变量值。

你不能:这就是受保护的全部意义。

您必须使用为您获取变量的方法来扩展该类。

但是,您不能在实例化的对象上执行此操作 - 您必须影响类定义,或者在创建对象时更改对象的类。

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.

只想待在家 2024-09-21 01:34:29

您可以在类外部访问类的受保护成员,也无需扩展受保护成员类,也无需使用受保护成员类的任何函数。使用下面的函数来访问它。

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];
}

请访问链接查看更多详细信息。

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.

枫林﹌晚霞¤ 2024-09-21 01:34:29

例如,使用闭包访问 php protected 变量

class ForExample
{
    protected $var=122;
}



$call=function(){

    echo $this->var;
};

$call->call(new ForExample());

with closure acces php protected variable for example

class ForExample
{
    protected $var=122;
}



$call=function(){

    echo $this->var;
};

$call->call(new ForExample());
怕倦 2024-09-21 01:34:29

如果您确实需要该值:

  • 修改类并添加一个返回您想要的值的公共方法。
  • 如果您无法修改它,请考虑扩展它并公开其中的值(它将是可访问的,因为它受到保护)。更喜欢第一个选项,这更像是一种黑客攻击。

显然,类设计者认为您不需要尝试访问的值,否则他会添加一个方法来自己检索它。因此,重新考虑一下你在做什么。

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.

夏天碎花小短裙 2024-09-21 01:34:29

免责声明:我不记得如何编码。已经“有一段时间了”。这可能完全关闭了。

嗯,首先,如果成员受到保护,那么最初的设计者并不打算让你直接访问它们。您检查过访问器方法吗?

如果没有,并且您确信确实需要这些受保护的成员,则可以使用访问器扩展类型,进行强制转换,然后以这种方式获取它们。就像(在类似 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 的编译器很糟糕,所以我无法测试它。
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文