duanjiati1755 2016-02-01 13:41
浏览 71
已采纳

使用主类的现有值实例化子类php

EDIT: Really sorry screwed up original example. More explanation at end.

I am new to OOP finding it useful, finding it confusing.

I have looked and I think this must be a simple question but searched on things like "instantiation of subclass php" but could find nothing on point.

I want to instantiate an object and then later instantiate the subclass. Can I "add" the subclass to the existing instance?

class Dog
{
    protected $owner;
    protected $color;

    public function setOwner($owner)
    {
        $this->owner = $owner;
    }
    public function setColor($color)
    {
        $this->color = $color;
    }
}

class Bulldog extends Dog
{
    protected $name;
    protected $typeOfTail;

... set tail....
    public function setName($name)
    {
        $this->name = $name;
    }
}

class Beagle extends Dog
{
    protected $name;
    protected $typeOfSpots;

... set spots ....
    public function setName($name)
    {
        $this->name = $name;
    }
}


$myDog = new Dog;
$myDog -> setOwner("Bob");

$myDog1 = new Beagle;
$myDog1 -> setName("name");

obviously that will not work but if I do

$myDog = new Beagle;
$myDog -> setName("name");

I presume that just sets the owner to NULL?

Is there a way to pull the existing values (or duplicate the values) of a class into and instance of a subclass (I suppose I could do some sort of complicated method to pull all the values in but there a lot of them ....) Is this something easy to do in PHP or am I off on a LIMB?

It is 3.30 am and I apologise if this is really dumb but I have hit a wall these last couple of days and am getting behind. This seems like it could be useful in the current project.

Clarification: This is a hypothetical example. (No dogs involved with the project.) Say we have a brown dog owned by Fred and we populate an instance of dog (pretend it is a big class with lots going on).

The next day someone says "that dog is a beagle" (ok later in the file - this is not a great example) so we want to instantiate a Beagle class with the name Suki.

What I want is an instance of Beagle that inherits the already existing dog info.

Sorry once again. Off to bed.

  • 写回答

2条回答 默认 最新

  • duanjiaonie6097 2016-02-01 13:51
    关注

    General solution: (in case you already know the future subclass)

    Just directly create an instance of the subclass. You can use it as an instance of the superclass. But some more information of what you're trying to achieve would be useful.

    So it would work like this:

    class Dog
    {
        protected $owner;
        protected $color;
    
        public function setOwner($owner)
        {
            $this->owner = $owner;
        }
        public function setColor($color)
        {
            $this->color = $color;
        }
    }
    
    class Beagle extends Dog
    {
        protected $name;
    
        public function setName($name)
        {
            $this->name = $name;
        }
    }
    
    
    $myDog = new Beagle;
    
    // here we call two methods of the Dog superclass
    $myDog->setOwner("Bob");
    $myDog->setColor("brown");
    
    // now we call a method of the subclass
    $myDog->setName("SomeName");
    

    As you can see there is no need to add the methods of the parent class to the subclass again. They will be inherited implicitly and you can just use them on instances of the subclasses as if they are defined within the subclass.

    If you omit one of these method calls the property will be null, because no default value is assigned to the properties.

    Specific solution: (in case you don't know the actual subclass)

    If you can't create the subclass directly, create a constructor like this:

    class Dog
    {
        protected $owner;
        protected $color;
    
        public function __construct(Dog $dog = null) {
            if ($dog) {
                foreach ($dog as $key => $value) {
                    if (property_exists($this, $key)) {
                        $this->{$key} = $value;
                    }
                }
            }
        }
    
        public function setOwner($owner)
        {
            $this->owner = $owner;
        }
        public function setColor($color)
        {
            $this->color = $color;
        }
    }
    
    class Beagle extends Dog
    {
        protected $name;
    
        public function setName($name)
        {
            $this->name = $name;
        }
    }
    
    $dog = new Dog;
    $dog->setOwner("Bob");
    
    $beagle = new Beagle($dog);
    $beagle->setColor("brown");
    

    I did not test the code, so maybe there is still a bug in it, but it should get my point accross.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行