访问类外部的受保护成员变量
我正在通过访问某人已经放置的类函数来查询字段的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
从公共访问受保护或私有变量是不正确的(这就是它们是受保护或私有的原因)。因此,更好的方法是扩展类并访问所需的属性,或者创建 getter 方法来公开获取它。但是,如果您仍然想在不扩展的情况下获取属性,并且使用的是 PHP 5,则可以使用 反射类。实际上尝试 ReflectionProperty 类。
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.
这是正确的答案:
我们可以使用 Closure 类的bind()或bindTo方法来访问某个类的私有/受保护的数据,例如:
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:
只需向类添加一个“get”方法即可。
Just add a "get" method to 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.
您可以在类外部访问类的受保护成员,也无需扩展受保护成员类,也无需使用受保护成员类的任何函数。使用下面的函数来访问它。
请访问链接查看更多详细信息。
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.
Please visit the Link to check more details about it.
例如,使用闭包访问 php protected 变量
with closure acces php protected variable for example
如果您确实需要该值:
显然,类设计者认为您不需要尝试访问的值,否则他会添加一个方法来自己检索它。因此,重新考虑一下你在做什么。
If you really need that value:
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.
免责声明:我不记得如何编码。已经“有一段时间了”。这可能完全关闭了。
嗯,首先,如果成员受到保护,那么最初的设计者并不打算让你直接访问它们。您检查过访问器方法吗?
如果没有,并且您确信确实需要这些受保护的成员,则可以使用访问器扩展类型,进行强制转换,然后以这种方式获取它们。就像(在类似 C++ 的代码中)
然后使用它
你就明白了。希望这对您有用,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)
and then to use it
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.
}