2015-11-22 53 views
-2

它返回的垃圾輸出。我是PHP新手。請幫我解決這個問題。我不想永遠陷入這種困境。我是面向對象編程的新手,所以我無法弄清楚哪裏出了問題和哪裏!爲什麼我這個php代碼出錯了?

<?php 
    Abstract class Employee { 
     protected $Fname; 
     protected $Lname; 
     protected $dept; 
     static protected $total= '0'; 
     public abstract function getSalary(); 
     function __construct($f,$l,$d) { 
      $this->Fname = $f; 
      $this->Lname = $l; 
      $this->dept = $d; 
      self::$total++; 
     } 
     public function getFullname() { 
     return $this->Fname." ".$this->Lname;  
     } 
     public function getDept() { 
     return $this->dept; 
     } 
     public static function getTotal(){ 
      return self::$total; 
     } 
    } 
    class FullTime extends Employee { 
     protected $Annualsalary; 
     function __construct($s) { 
      $this->Annualsalary = $s; 
     } 
     public function getSalary() { 
      return $this->Annualsalary/12; 
     } 
    } 
    class Contract extends Employee { 
     protected $Monthlypay; 
     function __construct ($s) 
     { 
      $this->Monthlypay = $s; 
     } 
     public function getSalary() { 
      return $this->Monthlypay; 

     } 
    } 


     $emp1 = new FullTime("John",'Doe','IT',150000); 
     $emp2 = new FullTime('J','Doe','sales',130000); 
     $emp3 = new FullTime('John','D','sales',140000); 
     $emp4= new Contract('JOHAN','Doe','sales',14000); 
     echo "Total Employees = ".Employee::getTotal()."<br>"; 
     echo $emp1->getFullname()." | ".$emp1->getDept()." | ".$emp1->getSalary()."<br>"; 
     echo $emp2->getFullname()." | ".$emp2->getDept()." | ".$emp2->getSalary()."<br>"; 
     echo $emp3->getFullname()." | ".$emp3->getDept()." | ".$emp3->getSalary()."<br>"; 
     echo $emp4->getFullname()." | ".$emp4->getDept()." | ".$emp4->getSalary(); 
     ?> 

輸出

僱員總數= 0

| | 0

| | 0

| | 0

| | JOHAN

+2

能否請您解釋一下您的問題實際上是什麼? –

+0

你真的得到了什麼錯誤? – Imran

+0

我編輯了這個問題。請看看並幫助我 –

回答

2

You are close。您需要初始化每個類屬性,PHP不會自動執行此操作。只需在父類中調用構造函數即可。另外,您需要將所有參數傳遞給構造函數。

看一看這樣的:

<?php 
 
    
 
    Abstract class Employee { 
 
     protected $Fname; 
 
     protected $Lname; 
 
     protected $dept; 
 
     static protected $total = 0; 
 
    
 
     public abstract function getSalary(); 
 
    
 
     function __construct($f, $l, $d) { 
 
      $this->Fname = $f; 
 
      $this->Lname = $l; 
 
      $this->dept = $d; 
 
      self::$total++; 
 
     } 
 
    
 
     public function getFullname() { 
 
      return $this->Fname . " " . $this->Lname; 
 
     } 
 
    
 
     public function getDept() { 
 
      return $this->dept; 
 
     } 
 
    
 
     public static function getTotal() { 
 
      return self::$total; 
 
     } 
 
    } 
 
    
 
    class FullTime extends Employee { 
 
     protected $Annualsalary; 
 
    
 
     function __construct($f, $l, $d, $s) { 
 
      parent::__construct($f, $l, $d); 
 
      $this->Annualsalary = $s; 
 
     } 
 
    
 
     public function getSalary() { 
 
      return $this->Annualsalary/12; 
 
     } 
 
    } 
 
    
 
    class Contract extends Employee { 
 
     protected $Monthlypay; 
 
    
 
     function __construct($f, $l, $d, $s) { 
 
      parent::__construct($f, $l, $d); 
 
      $this->Monthlypay = $s; 
 
     } 
 
    
 
     public function getSalary() { 
 
      return $this->Monthlypay; 
 
    
 
     } 
 
    } 
 
    
 
    
 
    $emp1 = new FullTime("John", 'Doe', 'IT', 150000); 
 
    $emp2 = new FullTime('J', 'Doe', 'sales', 130000); 
 
    $emp3 = new FullTime('John', 'D', 'sales', 140000); 
 
    $emp4 = new Contract('JOHAN', 'Doe', 'sales', 14000); 
 
    
 
    echo "Total Employees = " . Employee::getTotal() . "<br>"; 
 
    echo $emp1->getFullname() . " | " . $emp1->getDept() . " | " . $emp1->getSalary() . "<br>"; 
 
    echo $emp2->getFullname() . " | " . $emp2->getDept() . " | " . $emp2->getSalary() . "<br>"; 
 
    echo $emp3->getFullname() . " | " . $emp3->getDept() . " | " . $emp3->getSalary() . "<br>"; 
 
    echo $emp4->getFullname() . " | " . $emp4->getDept() . " | " . $emp4->getSalary(); 
 
    ?>

+0

非常感謝。先生,你真棒。我也被混淆了來自孩子課的爭論。這使我的觀念變得清晰。瘋狂的尊重你。 –

0

__constructor是全職類覆蓋,但是不能調用父類的構造

function __construct($f,$l,$d, $s) { 
parent::__construct($f,$l,$d); 
$this->Annualsalary = $s; 
} 
相關問題