PHP设置父类属性可以工作但后续if语句不起作用

时间:2017-03-19 10:01:20

标签: php oop

我正在构建一个遗传计算器,并将我的代码缩减为一种简单的格式来解释我的问题。

我基本上有这行实例化一个阴影对象:

$hatch = new Hatch($maleGeneticsPOST, $femaleGeneticsPOST, 'leopardGecko', true);

这需要父遗传学的表格帖子并设置物种类型。下面是我的Parent类和Child类,以显示它本质上是如何工作的:

class Genetics
{
    public $species = '';
    public $dominants = [];
    public $recessives = [];
    public $snows = [];
    public $wildtypes = [];

    function __construct($species)
    {
        $this->species = $species;
        echo $species; // returns leopardGecko as expected
    }
}

class Hatch extends Genetics
{
    function __construct($father, $mother, $species, $autoHatch = true, $hatchMethod = "punnett")
    {
        parent::__construct($species);
        // Other code for $father, $mother etc.
    }
}

从表面上看,这2个类彼此之间运作良好,我可以在对象中设置物种类型,Hatch将父类设置为它。

然而,我正在努力做的是然后使用父母中的$species属性来设置遗传,基于所选择/设置的物种;这是一个例子:

class Genetics
{
    public $species = '';
    public $dominants = [];
    public $recessives = [];
    public $snows = [];
    public $wildtypes = [];

    function __construct($species)
    {
        $this->species = $species;
        echo $species; // returns leopardGecko as expected

        if($species === "leopardGecko"){
            $this->dominants = ['NN', 'BB', 'TT'];
            $this->recessives = ['Bb', 'Tt', 'Rr'];
            $this->snows = ['Mm', 'Gg'];
            $this->wildtypes = ['QQ', 'Qq'];
        }
    }
}

当我尝试在我的Hatch类中进一步使用它们时,它们只返回空数组:

foreach ($alleles as $allele) {
    //echo $this->allGenetics[$allele].' ';
    if (in_array($allele, $this->dominants, true)) {
        //echo $this->allGenetics[$allele].' ';
        array_push($geckoGenetics['Gecko']['Dominants'], $this->allGenetics[$allele]);
        array_push($geckoGenetics['Gene']['Dominants'], $allele);
    } elseif (in_array($allele, $this->recessives, true)) {
        //echo $this->allGenetics[$allele].' ';
        array_push($geckoGenetics['Gecko']['Recessives'], $this->allGenetics[$allele]);
        array_push($geckoGenetics['Gene']['Recessives'], $allele);
    } elseif (in_array($allele, $this->wildtypes, true)) {
        //echo $this->allGenetics[$allele].' ';
        array_push($geckoGenetics['Gecko']['Wildtypes'], $this->allGenetics[$allele]);
        array_push($geckoGenetics['Gecko']['Recessives'], $this->allGenetics[$allele]);
        array_push($geckoGenetics['Gene']['Wildtypes'], $allele);
        array_push($geckoGenetics['Gene']['Recessives'], $allele);
    } elseif (in_array($allele, $this->snows, true)) {
        array_push($geckoGenetics['Gecko']['Snows'], $this->allGenetics[$allele]);
        array_push($geckoGenetics['Gene']['Snows'], $allele);
    }
}

请注意 :该代码的其余部分工作正常,我只是在谈论$this->dominants$this->recessives,{ {1}}& $this->wildtypes个变量 - 它们返回空白。

我错过了一些明显的东西吗?这是我第一次正确去OOP,它除了这一点之外还顺利!

1 个答案:

答案 0 :(得分:1)

'您调用字段$species的方式与使用$dominants的方式有所不同。如果您想访问函数范围之外的字段,则需要使用$this->调用它们。

所以在构造函数中,如果替换以下内容:

public $dominants = ['NN', 'BB', 'TT'];
public $recessives = ['Bb', 'Tt', 'Rr'];
public $snows = ['Mm', 'Gg];
public $wildtypes = ['QQ', 'Qq'];

使用:

$this->dominants = ['NN', 'BB', 'TT'];
$this->recessives = ['Bb', 'Tt', 'Rr'];
$this->snows = ['Mm', 'Gg'];
$this->wildtypes = ['QQ', 'Qq'];

它应该有用。