2016-12-03 29 views
1

在類聲明中使用構造函數和直接屬性賦值分配屬性值有什麼區別?換句話說,以下兩段代碼爲新對象創建默認值有什麼區別?在類聲明中使用構造函數和屬性賦值分配屬性值有什麼區別?

代碼直接分配:

<?php 
class A { 
    public $name="aName"; 
    public $weight = 80; 
    public $age = 25; 
    public $units = 0.02 ; 
} 
?> 

代碼構造:

<?php 
class A { 
    public $name; 
    public $weight; 
    public $age; 
    public $units; 
    public function __construct() { 
     $this->name = "aName"; 
     $this->weight = 80; 
     $this->age = 25; 
     $this->units= 0.02 ; 
    } 
} 
?> 

你可能會回答,我不能改變硬編碼的性能,但我可以在下面的代碼(在Local Sever):

<?php 
    class A{ 
    public $name="aName"; 
    public $weight = 80; 
    public $age = 25; 
    public $units = 0.02 ; 
    } 
class B extends A{ 
    public function A_eat(){ 
     echo $this->name.' '."is".' '.$this->age.' '."years old<br>"; 
     echo $this->name.' '."is eating".' '.$this->units.' '."units of food<br>"; 
     $this->weight +=$this->units; 
     echo $this->name.' '."weighs".' '.$this->weight."kg"; 
    } 
    } 
    $b = new B(); 
    echo "<p>If no changes to the object's Properties it inherits the main class's</p>"; 
    $b->A_eat(); 
    echo '<br><br>'; 
    echo "<p>If changes made to the object's Properties it uses it's new properties</p>"; 
    $b->name ="bName"; 
    $b->weight = 90; 
    $b->units = 0.05; 
    $b->A_eat(); 
?> 
+0

這裏有什麼問題?對於一個你的構造方法是不正確的,你錯過了你的支架括號減速,爲什麼你會公開一些東西,如果你使用一個方法來訪問它在setter/getter? – KDOT

+0

您的示例中充滿了語法錯誤(例如'$ weight->')。這就是說,你不能在沒有構造函數的情況下動態設置值。 – Qirel

+0

這是我的錯誤,我添加了括號。但我的問題是爲什麼要使用構造函數,我可以給該屬性聲明中的默認值?..請忘記語法。 –

回答

2

當屬性聲明包含初始化時,在編譯時間處,即在將PHP源編譯爲PHP opcodes的步驟中評估初始化。

在構造函數中的代碼在運行時間處被評估,即在用new運算符創建對象時。

如果您不使用操作碼高速緩存(OPcache,APC和類似的擴展),幾乎沒有區別。但是,如果操作碼被緩存,顯然,編譯時初始化的性能會更好。

0

然而,沒有什麼,但...

在結構(),你可以通過在可選變量...例如..

public function __construct($name = 'aName', $weight = 80, $age = 25, $units = 0.02) { 
    $this->name = $name; 
    $this->weight = $weight; 
    $this->age = $age; 
    $this->units = $units; 
} 

在您發佈,要定義HARD值的特性,第一個例子是在第二一個,構建會讓你 「打造」 那些......

在第一個例子:

$x = new Class(); 
// You can't change it, those properties are what you coded in... 

現在看看我的例子...

$x = new Class('My New Name', 120, 30, 0.10); 
// Now $name = 'My New Name' etc... 

的結構可以讓你建立在初始化的對象,而不是堅硬如你有編碼的默認值...

你可以使用默認值,並創建制定者,但我喜歡的__construct()

0

這兩者之間沒有什麼大的區別。

這兩種方法都很好,它基本上取決於您的使用情況。

根據第一種方法你都無法將值分配給類的成員,而在第二個方法創建一個類的對象,而你有力量,讓我證明這一點:在方法上第二

class A 
{ 
    protected $name = 'John'; 
    protected $age = 20; 
} 

$obj = new A(); 
/* Here you get an object with 
** $obj->name = 'John' 
** $obj->age = 20; 
*/ 

而你具有在旅途中定義變量的能力(在定義其對象時)。每次你創建B類的對象,你可以根據你的傳遞參數給類的構造函數具有不同的對象,每次:它有一個構造方法調用每個 新創建的對象上此方法

class B 
{ 
    protected $name; 
    protected $age; 

    public function __construct($name = 'John', $age = 20) 
    { 
     $this->name = $name; 
     $this->age = $age; 
    } 
} 

$obj = new B("John Doe", 40); 
/* Here you get an object with 
** $obj->name = 'John Doe' 
** $obj->age = 40; 
*/ 

$obj = new B("John Doe"); 
/* Here you get an object with 
** $obj->name = 'John Doe' 
** $obj->age = 20; 
*/ 

類,所以它適用於在使用之前對象可能需要的任何初始化

希望這有助於!

0

因爲構造函數可以接收參數以使對象以某些默認值以外的值開始。沒有帶參數的構造函數,你必須創建一個默認對象,然後修改它的字段。

還要注意總有一個構造函數;這只是一個問題,你是否自己創建了一個,或者爲你創建了一個不需要任何參數的默認的問題。

0

在這種情況下,構造函數只是將對象置於活動狀態,分配適當的內存。

可以創建一個Person對象,只需提供一個名稱作爲參數,但當名稱和年齡都作爲參數給定時,可能需要另一個構造函數。

在PHP中,你被限制爲1不幸的是,_contruct

您可以在周圍的工作方式shown here但內置多個構造。

0

構造方法和對象屬性之間的區別和什麼時候應該使用它們?

首先,construct()返回比如,你正在使用的類,因此爲什麼如果你曾經嘗試返回的東西這個方法裏面不同:

public function __construct() { return 'hello, world!'; } 

它不會實際工作。你可能在問這是什麼意思,爲什麼它對於這個問題如此重要。那麼,有不止兩種方式來訪問屬性中的數據,有些東西我們稱爲獲取者設置者

private $_name; 
public function setName($name) { $this->_name = $name; return $this; } 
public function getName() { return $this->_name; } 

在您的使用情況,我認爲你的數據是不不斷,它並不總是會成爲25時代,所以你可以使用多種方式來處理這一點。如果數據來自數據庫,構造方法將加載像這樣的配置是有用的:

public $name; 
public function __construct($id) { 
    $user = SomeDriver::GetInstance()->on('Users', '*')->where('user_id = ?', [$id]); 
    $this->name = $user['username']; 
} 

然後你就可以只通過像$obj->name實例訪問。但是,如果您只是通過輸入設置數據,則可以使用setter/getter或直接訪問範圍外的屬性;

class User { 
    public $name = 'Frank'; 
    public function setName($name) { $this->name = $name; return $this; } 
    public function getName() { return $this->name; } 
} 

$u = new User; 
echo $u->name; 
$u->name = 'John'; 

// or change the property to private and: 

$u = new User; 
echo $u->getName(); 
$u->setName('John'); 
相關問題