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条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵