2012-05-13 198 views
4

查看網絡上有關PHP工廠模式的示例。爲什麼我需要「&」?

在行$kind =& Vehicle::category($wheel);,我爲什麼要使用&

代碼:

<?php 
    class Vehicle { 

     function category($wheel = 0) { 
      if ($wheel == 2) { 
       return "Motor"; 
      } elseif($wheel == 4) { 
       return "Car"; 
      } 
     } 
    } 

    class Spec { 

     var $wheel = ''; 

     function Spec($wheel) { 
      $this->wheel = $wheel; 
     } 

     function name() { 
      $wheel = $this->wheel; 
       return Vehicle::category($wheel); 
     } 

     function brand() { 
      $wheel = $this->wheel; 
       $kind =& Vehicle::category($wheel); 
      if ($kind == "Motor") { 
       return array('Harley','Honda'); 
      } elseif($kind = "Car") { 
       return array('Nisan','Opel'); 
      } 
     } 
    } 

    $kind = new Spec(2); 
    echo "Kind: ".$kind->name(); 
    echo "<br>"; 
    echo "Brand: " . implode(",", $kind->brand()); 
?> 
+0

警告:行'} ELSEIF($ KIND = 「汽車」){''需要==',而不是'='。 – TaZ

+0

我沒有看到,直到你發佈Thx。 –

回答

4

這是沒用在這裏,因爲例子越來越以一個恆定的參考,但references也有其用途,當你想「看」,可能改變一個值,你希望你的變量隨之改變。