将对象从父级传递给子级
我目前正在使用 IP.Board 框架开发一个自定义应用程序,该框架采用 PHP 语言,默认情况下为登录用户创建一个 IPSMember
对象。但是,我正在开发一个附加类,基本上
class SpecialUser extends IPSMember
有没有办法获取父对象,即 IPSMember
更改为 SpecialUser
?
I'm currently developing an Custom Application using the IP.Board framework, which is in PHP, which by default, creates a IPSMember
object for the logged-in user. However, I'm developing an additional class, basically
class SpecialUser extends IPSMember
Is there a way to get the parent object, which is IPSMember
to change to SpecialUser
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定,但我不相信有办法在内部更改对象类型。至少,我无法让 __construct() 返回不同类的对象。
最简单的方法也许是在 SpecialUser 中创建一个静态初始化方法,该方法接受 IPSMember 对象并转换属性,返回 SpecialUser 对象。
Reflection Class 的 getproperties 方法可以让您快速完成此操作。 http://php.net/manual/en/reflectionclass.getproperties.php
希望有人能为您提供更快的解决方案。
祝你好运。
I'm not certain, but I do not believe there is a way to change the object type internally. At least, I've been unable to have a __construct() return an object of a different class.
The easiest way, perhaps, would be to create a static initializer method in SpecialUser which takes in an IPSMember object and translates the properties, returning a SpecialUser object.
The Reflection Class's getproperties method may enable you do to this quickly. http://php.net/manual/en/reflectionclass.getproperties.php
Hopefully someone can offer you a quicker solution.
Good luck.