开发者

Transferring object from parent to child

开发者_如何学JAVA

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜