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 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
  • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
  • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?
  • ¥15 QQ邮箱过期怎么恢复?
  • ¥15 登录他人的vue项目显示服务器错误
  • ¥15 (标签-android|关键词-app)
  • ¥15 comsol仿真压阻传感器