如何获取父对象';php中的子类中的s属性?

如何获取父对象';php中的子类中的s属性?,php,oop,parent-child,Php,Oop,Parent Child,请注意,我已经有一段时间没有尝试php和oop了 我希望帮助我的合作伙伴跟踪我们的巨大魔法:收集卡片。 在魔术中,通常情况下,卡片以扩展块的形式发行。在一个区块内有三个扩展。第一个扩展与整个块具有相同的名称、符号等,它们实际上或多或少是相同的,但我希望在代码中对块和扩展进行区分 同时,我希望在向块添加第一个扩展时避免两次输入相同的信息 $block = new Block('Innistrad', 'isd', '130927'); $exp = $block->addExpansion(

请注意,我已经有一段时间没有尝试php和oop了

我希望帮助我的合作伙伴跟踪我们的巨大魔法:收集卡片。 在魔术中,通常情况下,卡片以扩展块的形式发行。在一个区块内有三个扩展。第一个扩展与整个块具有相同的名称、符号等,它们实际上或多或少是相同的,但我希望在代码中对块和扩展进行区分

同时,我希望在向块添加第一个扩展时避免两次输入相同的信息

$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)方法中设置的

不知何故,我想在扩展构造函数中添加一个条件来比较扩展名和块名。如果它们相等,则扩展符号与块符号相同,否则在构造函数方法中设置扩展符号。我尝试使用
$block=get\u parent\u类($this)$this->name==$block->name
作为条件,但(当然?)这并没有按预期工作,扩展符号“input”为空?符号不是“isd.png”,而是“.png”

“完成”代码,其中包含我尝试过但失败的类和注释

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

你在虐待孩子的父母。如果您创建一个
扩展
只有一个对象,
扩展
本身就是一个对象。扩展父类并不意味着将创建父类的
实例
,它只是使子类继承父类的所有功能。 您应该将
addExpansion
方法和
扩展的构造更改为以下内容:

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


我想我意外地解决了我的问题。我没有在扩展构造函数中声明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,因为setParentBlock在构造之后才被调用)

我怀疑您是否需要扩展类来扩展Block类,但我还是保留了这一点——如果您注释掉“extensdblock”,它也可以工作(更好,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);

你是用另一种方式做的。我建议您将扩展存储在一个数组中,这样您就可以更容易地跟踪它们。谢谢您,我会这样做的。=)这是我的代码的一个(非常)简化的版本,用来强调和关注前一个(?)问题。我怀疑我使用的亲子关系有问题。我尝试了上述建议,但从未得到将$symbol扩展为等于Block$symbol的预期结果。
// 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
)
// 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);