2016-04-21 115 views
0

所以我有这个小类将流体的属性存储在一起。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 

无视类流体的所有值。我假设值都是NULL,我的初始化不正确,因为我是PHP新手。但是,我无法弄清楚我的语法有什么问题。

回答

0

。在你的代码中的语法错误,你需要做的双下划线构建调用构造函数

__construct 

构造函数没有被调用,因为在对象创建这个语法错误。

1

问题是你在__construct方法中只有一个下划线。

_construct应该是__construct