如何将PHP对象复制到其他对象类型 [英] How do you copy a PHP object into a different object type

查看:60
本文介绍了如何将PHP对象复制到其他对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 新类是原始对象的子类

  1. New class is a subclass of the original object

它必须与php4兼容

推荐答案

您可以将您的类实例化为空,然后通过任意数量的方法加载.这些方法之一可以接受父类的实例作为参数,然后从那里复制其数据

You could have your classes instantiated empty and then loaded by any number of methods. One of these methods could accept an instance of the parent class as an argument, and then copy its data from there

class childClass extends parentClass
{
    function childClass()
    {
        //do nothing
    }

    function loadFromParentObj( $parentObj )
    {
        $this->a = $parentObj->a;
        $this->b = $parentObj->b;
        $this->c = $parentObj->c;
    }
};

$myParent = new parentClass();
$myChild = new childClass();
$myChild->loadFromParentObj( $myParent );

这篇关于如何将PHP对象复制到其他对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆