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 R语言单细胞测序 对seurat对象进行标准化时出现问题
  • ¥15 Anaconda navigator updateor一直更新
  • ¥15 请问如何在matlab里使用raven工具?
  • ¥100 关于ios手游充值到一定金额,再点充值提示公众号的问题。
  • ¥15 求一个stm32串口控制程序
  • ¥20 Windows 驱动开发版本疑问相关
  • ¥15 MAC 未能打开磁盘映像
  • ¥15 fastcap使用,二维导体输入问题
  • ¥15 hosts修改后不能访问
  • ¥15 关于化学反应速率C++编译问题/FLUENT