将对象从父级传递给子级

发布于 2024-08-29 18:20:46 字数 245 浏览 2 评论 0原文

我目前正在使用 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 技术交流群。

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

发布评论

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

评论(1

夜司空 2024-09-05 18:20:46

我不确定,但我不相信有办法在内部更改对象类型。至少,我无法让 __construct() 返回不同类的对象。

最简单的方法也许是在 SpecialUser 中创建一个静态初始化方法,该方法接受 IPSMember 对象并转换属性,返回 SpecialUser 对象。

class SpecialUser extends IPSMember
{
    public static function initWithIPSMember (IPSMember $ipsMember)
    {
        $specialUserObj = new SpecialUser();
        // translate any properties     
        return $specialUserObj;
    }
}

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.

class SpecialUser extends IPSMember
{
    public static function initWithIPSMember (IPSMember $ipsMember)
    {
        $specialUserObj = new SpecialUser();
        // translate any properties     
        return $specialUserObj;
    }
}

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.

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