初始化后,PHP类属性为null

时间:2016-04-21 03:11:55

标签: php

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

 <?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的新手。但是,我无法弄清楚我的语法有什么问题。

2 个答案:

答案 0 :(得分:1)

问题是你是__construct方法中的一个下划线短。

_construct应为__construct

答案 1 :(得分:0)

您的代码中存在语法错误,您需要为调用构造函数

创建双下划线构造
__construct

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