2015-06-21 31 views
-3

我敢打賭,這對你來說很簡單,但它仍然令我困惑,我想在繼續學習更先進的東西之前理解這一點。所以我希望你能爲我說清楚。PHP簡單功能,需要一些解釋

的代碼如下:

​​

使代碼工作很好,但我不明白,這部分究竟是如何工作的:

public function NumberInput($number) 
{ 
    $this->number = $number; 
} 

如果我想例如輸入一些號碼是什麼,但我不想將它保存在另一個變量中,例如變量$ a。這不起作用(我不知道爲什麼):

public function NumberInput($number) 
{ 
    $this->number = $a; 
} 

對此的任何解釋?

在此先感謝。

+1

建議閱讀:http://php.net/manual/en/language.oop5.basic.php – nicael

+0

[參考 - 這個符號在PHP中意味着什麼?](http://stackoverflow.com/問題/ 3737139 /參考什麼 - 做 - 這個符號,均在-PHP) –

回答

0

NumberInput接受一個參數($ number)並將其分配給該對象的一個​​屬性。

在您的示例中,$ b是對象示例的一個實例。對象$ b(和類示例)有兩個方法NumberInput和NumberOutput,它們作用於屬性$ number。

當您使用數字調用NumberInput時,它將通過參數$ number分配屬性$ number以傳入的值。

$ this-> number = $ a不起作用,因爲$ a沒有定義並且不作爲參數傳入。

0
class example{ 

    public function NumberInput($number){ 
     // this line takes the function parameter $number 
     // and creates a property of this object on the fly 
     // ALSO called number ($this->number) 
     $this->number = $number; 
    } 


    public function NumberOutput(){ 
     // this returns the value in the property 
     // of this object called number 
     return $this->number; 
    } 

    } 

$b = new example; 
$b->NumberInput(7); 
echo $b->NumberOutput(); 

倒不如這樣寫的

class example{ 

    // this is the number property defined 
    // rather than being created on the fly in NumberInput 
    // defined as private as you have a GETTER function that 
    // users of the class shoudl use to see its value so it sort of 
    // implies it should not be available as a public property 
    private $number; 

    public function NumberInput($number){ 
     // move parameter $number value to my property called number 
     $this->number = $number; 
    } 


    public function NumberOutput(){ 
     // this returns the value in the property 
     // of this object called number 
     return $this->number; 
    } 

    } 

$b = new example; 
$b->NumberInput(7); 
echo $b->NumberOutput(); 

這不起作用

public function NumberInput($number) 
{ 
    $this->number = $a; 
} 

,因爲那裏是$一個定義,它不存在

但是,您可能做

public function NumberInput($a) 
{ 
    $this->number = $a; 
} 

或者

public function NumberInput($number) 
{ 
    $this->a = $number; 
} 
0

我建議以下幾個標準(幾乎普遍使用),面向對象的代碼慣例,這將使你的代碼更易讀,並應更清楚地給你它是如何工作:

  1. 將類名大寫,不要大寫函數名
  2. 總是聲明成員變量(通過$ this->來訪問)。 PHP允許您在不聲明它們的情況下使用它們,但聲明它們會使代碼更具可讀性,並使IDE等工具可以幫助您完成代碼完成等。
  3. 使用有意義的變量名稱並且不會根據變量名稱命名變量這可以改變,並在PHP的情況下可以混合)。所以'號碼'沒有意義,像'id','numChildren'和'年齡'這樣的名字是有意義的。
  4. 始終使用名爲setXXX和getXXX的方法來訪問成員變量。這種方法的標準術語是'訪問者'。

使用這些約定,在這裏與你相似,可以採取數值並將其保存在不同的成員變量::

class Example 
{ 
    private $id; 
    private $age; 
    private $numChildren 

    public function setId($number) 
    { 
     $this->id = $number; 
    } 

    public function getId() 
    { 
     return $this->id; 
    } 

    public function setAge($number) 
    { 
     $this->age = $number; 
    } 

    public function getAge() 
    { 
     return $this->age; 
    } 

    public function setNumChildren($number) 
    { 
     $this->numChildren = $number; 
    } 

    public function getNumChildren() 
    { 
     return $this->numChildren; 
    } 
} 

NB的示例類 - 請注意,我不會真的提倡用$ number作爲setter方法參數的名稱,但我已經用它來說明如何將一個數字保存到一個類的不同成員變量中。