繁体   English   中英

如何在php的子类中获取父对象的属性?

[英]How to get parent object's property inside child class in php?

请注意,距离我尝试过php和oop已经有一段时间了...

我希望帮助我的伴侣跟踪我们大量的《魔术:聚会》卡片收藏。 通常,在Magic中,卡在扩展块中释放。 在一个块内有三个扩展。 第一个扩展名与整个块具有相同的名称,符号等,它们实际上或多或少是相同的,但是我希望在代码中对块和扩展进行区别。

同时,我想避免在将第一个扩展添加到块时两次输入相同的信息。

$block = new Block('Innistrad', 'isd', '130927');
$exp = $block->addExpansion(new Expansion('Innistrad', 'isd', '130927')); // not DRY!
$firstExp = $block->addExpansion(new Expansion('Innistrad')); // This is more DRY, only name is needed

所有扩展都具有与块相同的轮换日期。 我设法在addExpansion(Expansion $ exp)方法中进行设置;

$exp->rotationDate = $this->rotationDate;

我想以某种方式在Expansion构造函数中添加一个条件,以将扩展名与块名进行比较。 如果它们相等,则扩展符号与块符号相同,否则在构造方法中设置扩展符号。 我尝试使用$block = get_parent_class($this); 然后$this->name == $block->name作为条件,但是(当然?)这没有按预期工作,并且扩展符号“ input”为空? 代替“ isd.png”的是符号“ .png”。

Notice: Trying to get property of non-object in - on line 178
Expansion Object
(
    [name:protected] => Innistrad
    [symbol:protected] => .png
    [rotationDate] => 130927
)

我尝试过和失败过的带有类和注释的“完成”代码...

// BLOCK
class Block {
    protected static $imgType = '.png';
    protected $name;
    protected $symbol;

    public function __construct($name, $symbol, $rotationDate) {
        $this->name = $name;
        $this->symbol = $symbol.self::$imgType;
        $this->rotationDate = $rotationDate;
    }

    public function addExpansion(Expansion $exp) {
        $exp->rotationDate = $this->rotationDate;
        return $exp;
    }
}

// EXPANSION
class Expansion extends Block {
    public function __construct($name, $symbol = null) {
        $this->name = $name;
        $block = get_parent_class($this); // this is what I tried, the principle of what I try to achieve
        if ($this->name == $block->name) {
        // if the instantiated child object has the same name as the parent object, "adopt" the parent object's properties
            $this->symbol = $block->symbol;
        }
        else {
            $this->symbol = $symbol.parent::$imgType;
        }
    }
}
$block = new Block('Innistrad', 'isd', '130927');
$exp = $block->addExpansion(new Expansion('Innistrad'));
print_r($exp);

您要虐待孩子-这里是父母的事情。 如果您创建一个Expansion ,则只有一个对象,即Expansion本身。 扩展父类并不意味着将创建父类的instance ,而只是使子类继承父类的所有功能。 您应该将addExpansion方法和Expansion的构造更改为如下所示:

public function addExpansion(Expansion $exp) {
    $exp->setParentBlock($this);
    $exp->rotationDate = $this->rotationDate;
    return $exp;
}

class Expansion extends Block {
    private $parent = null;

    public function setParentBlock(Block $b) {
      $this->parent = $b;
    }
    public function __construct($name, $symbol = null) {
        $this->name = $name;
        //create getName() because u can't access protected outside the class
        if ($this->parent != null && $this->name == $parent->getName()) {
        // if the instantiated child object has the same name as the parent object, "adopt" the parent object's properties
            $this->symbol = $block->symbol;
        }
        else {
            $this->symbol = $symbol.parent::$imgType;
        }
    }
}

我想我不小心解决了我的问题。 我没有在Expansion构造函数中声明if条件,而是将其放在Block类的addExpansion()方法中。 这给出了“采用”属性值的预期结果,但是我不确定这是否是正确的解决方案。

// BLOCK
class Block {
    protected static $imgType = '.png';
    protected $name;
    protected $symbol;
    protected $rotationDate;

    public function __construct($name, $symbol, $rotationDate) {
        $this->name = $name;
        $this->symbol = $symbol.self::$imgType;
        $this->rotationDate = $rotationDate;
    }

    public function addExpansion(Expansion $exp) {
        $exp->rotationDate = $this->rotationDate;
        if ($exp->name == $this->name) { // if Expansion name equals Block name
            $exp->symbol = $this->symbol; // use Block symbol as Expansion symbol
        }
        return $exp;
    }
}

// EXPANSION
class Expansion extends Block {
    public function __construct($name, $symbol = null) {
        $this->name = $name;
        $this->symbol = $symbol.parent::$imgType;
    }
}
$block = new Block('Innistrad', 'isd_exp_symbol', '20130927');
$exp = $block->addExpansion(new Expansion('Innistrad'));
print_r($exp);

返回值:

Expansion Object
(
    [name:protected] => Innistrad
    [symbol:protected] => isd_exp_symbol.png
    [rotationDate:protected] => 20130927
)

我之所以回答,是因为我认为@DarkBee的解决方案存在问题,但是我没有足够的声誉进行评论,所以我将提供一个完整的解决方案(问题是$ this-> parent在以下情况中始终为null是Expansion的构造函数,因为setParentBlock直到构造后才被调用)。

我对此表示怀疑,是否需要Expansion类来扩展Block类,但是无论如何,我都保留了它-如果您注释掉“ extends Block”,它也同样有效(更好,IMO)。 有任何问题请随时询问我(们。

// BLOCK
class Block {
    protected static $imgType = '.png';
    protected $name;
    protected $symbol;
    protected $expansions = array();

    public function __construct($name, $symbol, $rotationDate) {
        $this->name = $name;
        $this->symbol = $symbol.self::$imgType;
        $this->rotationDate = $rotationDate;
    }

    public function addExpansion($name, $symbol = null) {
        // We only have room for 3 expansions.
        if (count($this->expansions) >= 3) return null;

        // If the expansion to be created and added to this block has the
        // same name as this block, or if no symbol is supplied,
        // then "adopt" this block's symbol. 
        $symbol = ($name == $this->name || $symbol === null)
          ? $this->symbol
          : $symbol.self::$imgType;

        $exp = new Expansion($name, $symbol, $this->rotationDate);
        $this->expansions[] = $exp;

        return $exp;
    }
}

// EXPANSION
class Expansion extends Block {
    public function __construct($name, $symbolWithType, $rotationDate) {
        $this->name         = $name;
        $this->symbol       = $symbolWithType;
        $this->rotationDate = $rotationDate;
    }
}

$block = new Block('Innistrad', 'isd', '130927');
$exp = $block->addExpansion('Innistrad');
print_r($exp);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM