PHP set父类属性有效,但后续if语句无效';T

PHP set父类属性有效,但后续if语句无效';T,php,oop,Php,Oop,我正在构建一个遗传学计算器,并将代码简化为一种简单的格式来解释我的问题 我基本上有一行代码,它实例化了一个图案填充对象: $hatch = new Hatch($maleGeneticsPOST, $femaleGeneticsPOST, 'leopardGecko', true); 这将为父代遗传学设置一个表单post,并设置物种类型。下面是我的父类和子类,以说明这基本上是如何工作的: class Genetics { public $species = ''; public

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

我基本上有一行代码,它实例化了一个图案填充对象:

$hatch = new Hatch($maleGeneticsPOST, $femaleGeneticsPOST, 'leopardGecko', true);
这将为父代遗传学设置一个表单post,并设置物种类型。下面是我的父类和子类,以说明这基本上是如何工作的:

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.
    }
}
从表面上看,这两个类彼此工作良好,我可以在对象中设置物种类型,
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->隐性
$this->wildtypes
&
$this->snows
变量-它们返回空值


我错过了什么明显的东西吗?这是我第一次尝试OOP,除了这一点之外,一切都很顺利

“您如何调用字段
$species
,以及您如何使用
$dominants
进行调用,这两种方法是不同的。如果要访问函数范围之外的字段,需要使用
$this->
调用它们

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

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


它应该可以工作。

不幸的是,我仍然得到
Array()Array()
,因为输出我运行了代码,并且注意到
$this->snows中有一个字符串没有关闭(包含“Gg”的字符串)。对于其他人来说,它似乎起作用了。嗯,我肯定还有一些问题,因为它根本没有降低基因。我会再检查一些好的,看起来我有一些继承问题,但这个答案修复了一个初始问题,所以谢谢:)
$this->dominants = ['NN', 'BB', 'TT'];
$this->recessives = ['Bb', 'Tt', 'Rr'];
$this->snows = ['Mm', 'Gg'];
$this->wildtypes = ['QQ', 'Qq'];