初始化后PHP类属性为null


PHP Class Properties null after initializing

所以我有一个小类来存储流体的属性。

 <?php    
        // Two Phase flow vertical pressure differential calculator
        class Fluid {
                public $name;
                public $re;
                public $rho;
                public $j;
                public $D;
                public $f;
                public $dPdZ;
                public $w=0;

                public function _construct($arg1,$re,$rho,$j,$D){
                    //store inputs
                    $this->name=$arg1;
                    $this->re=$re;
                    $this->rho=$rho;
                    $this->j=$j;
                    $this->D=$D;
                    //calculate F value 
                    if($re < 1000){
                        $this->f = 16.0 / $re;
                    }elseif($re > 2000){
                        $this->f = .046 / pow($re, .2);
                    }else{
                        $this->w= ($re-1000)/1000;
                        $this->f= $this->w*16.0/$re+(1-$this->w)*.046/pow($re, .2);
                    }
                    //calculate Vertical pressure drop
                    $this->dPdZ=2*$this->f*$rho*$j*$j/$D+$rho*9.8;
                }
                // print contents of object
                public function  printOut(){
                    echo "For " . $this->name . "'r'n";
                    echo "Inputs: re=" . $this->re . " rho=".$this->rho . " j=" . $this->j . " D=" . $this->D . "'r'n";
                    echo "Intermediates: f=" . $this->f . " w=" . $this->w . " dP/dZ=" . $this->dPdZ . "'r'n";
                }
        }
        //create Fluid Objects  (currently static inputs)
        $liquid= new Fluid("liquid",111714.4,934.1,.5,.0508);
        $gas= new Fluid("gas",1201.2,.96,.5,.0508);
        //Find C
        if($liquid->re > 1500&& $gas->re > 1500){
            $C=20;
        }else if($liquid->re < 1500 && $gas->re > 1500){
            $C=12;
        }else if ($liquid->re > 1500 && $gas->re < 1500){
            $C=10;
        }else{
            $C=5;
        }
        //calculate pressure differential
        $dPdZ=$liquid->dPdZ+$gas->dPdZ+$C*pow($liquid->dPdZ*$gas->dPdZ,.5);
        //print results
        $liquid->printOut();
        $gas->printOut();
        echo "Yields: dP/dZ=". $dPdZ . " C=" . $C;
      ?>

然而,当我到达终点时,它会打印

For 
Inputs: re= rho= j= D=
Intermediates: f= w=0 dP/dZ=
For 
Inputs: re= rho= j= D=
Intermediates: f= w=0 dP/dZ=
Yields: dP/dZ=0 C=5

忽略Fluid类中的所有值。我假设这些值都是NULL,并且我的初始化是不正确的,因为我是PHP新手。然而,我不知道我的语法有什么问题。

问题是__construct方法中缺少一个下划线。

_construct应该是__construct

您的代码中存在语法错误,需要使用双下划线构造来调用构造函数

__construct

由于在创建对象时出现此语法错误,因此未调用您的构造函数。