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

时间:2014-11-09 12:14:57

标签: php oop parent-child

请注意,自从我尝试使用php和oop之后已经有一段时间......

我希望帮助我的伴侣跟踪我们庞大的Magic:The Gathering卡片系列。 通常,在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作为条件,但(当然?)这没有按预期工作,扩展符号"输入"一片空白?而不是" 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);

3 个答案:

答案 0 :(得分:0)

你在这里虐待孩子 - 父母的事。如果您创建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;
        }
    }
}

答案 1 :(得分:0)

我想我已经意外地解决了我的问题。我没有在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
)

答案 2 :(得分:0)

我回答是因为我认为@ DarkBee的解决方案存在问题,但我没有足够的声誉来发表评论,所以相反我会提供完整的解决方案。解决方案(问题是$ this-> parent在Expansion的构造函数中总是为null,因为setParentBlock在构造之后才被调用)。

我怀疑你需要扩展课程来扩展Block课程,但我还是离开了它 - 如果你注释掉它,那么它的效果也一样好(IMO)。扩展Block"。如有任何问题,请随时提出。

// 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);