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

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法