2015-05-30 36 views
-3
class Money { 
    private $something; 
    private $another; 

    public function getSomething() { 
     return $this->something; 
    } 

} 

class wallet extends Money{ 
    private $myAnother; 
    private $yourAnother; 
    private $money // An Object 

    public function setMoney($money) { 
     $this->$money = $money; 
     return $this; 
    } 

    public function getMoney() { 
     return $this->money; // Object 
    } 

} 

所以,我用這個:Class on PHP的這種用法是否正確?

$wallet = new Wallet(); 

print ($wallet ->getMoney()->getSomething()); 

工作正常!但我不知道是否是訪問電子錢包內部類Money的正確方法。

+0

改變'$本 - > $錢= $錢;''到$這個 - >錢= $錢;'。 – someOne

+0

還缺少一個';'在線私人$錢//一個對象' – Viral

+0

謝謝,這個想法是用法。我知道後 – Roger

回答

0

如果按照$money的字段您的意思是基類,那麼;這不是正確的路要走。您已經有權訪問子類的方法中的基類,因此$money的字段是多餘的,您應該將其刪除。

你可以使用__construct()方法初始化Money類,因此以下內容添加到您的Money類:

function __construct() { 
    $this->something = "something"; 
    $this->another = "another"; 
    // anything else needed to initialize this object of Money class.. 
} 

wallet類,除了直接初始化類中的字段,你需要初始化基類這樣的:

function __construct() { 
    parent::__construct(); 
    // anything else needed to initialize this object of wallet class.. 
} 

而且,作爲getSomething()在基類的Money方法具有public可見性,您可以直接使用$wallet->getSomething();來調用它。

+0

我應該檢查misstype和不正確的代碼。但是這個想法:我有一個類錢與方法,我有一個班錢包與另一種方法。當錢包有對象錢。我將訪問Money的方法,而無需在班級電子錢包上創建相同的方法。這可能嗎? – Roger

+0

@Roger當你通過繼承創建一個新類時,就像從'Money'類中派生'wallet'類一樣,那麼'wallet' **的對象實際上也是'Money'類的一個對象。除此之外,當您使用繼承時,客戶端可以通過派生類的對象訪問基類的所有公共方法,並且在派生類方法內部,您可以訪問所有非私有字段和方法。這就是說,在這個角度來看,你根本不需要'$ money'字段。 – someOne

+0

@Roger但是如果你需要使用「Money」類的一個對象作爲複合,那麼這完全是一個新故事,從這個角度來看,你根本不需要繼承! – someOne

0

我不太清楚你如何在沒有實例化Money對象的情況下工作,並且在代碼中存在一些拼寫錯誤。

但是無論如何,從你說的最後一行你想要「訪問錢包裏面的錢」,你的設置似乎沒有多大意義。錢包不是一種金錢,所以它看起來並不是正確的,它正在擴大資金。

如果你想要錢是一類,你可能會改爲使用依賴注入,像這樣:

class Money { 
    private $amount; 

    public function add($amount) { 
     $this->amount += $amount; 
    } 

    public function remove($amount) { 
     $this->amount -= $amount; 
    } 

    public function getAmount() { 
     return $this->amount; 
    } 
} 

class Wallet { 
    private $money; // An Object 

    public function __construct(Money $money) { 
     $this->money = $money; 
    } 

    public function addMoney($amount) { 
     $this->money->add($amount); 
    } 

    public function removeMoney($amount) { 
     $this->money->remove($amount); 
    } 

    public function getMoney() { 
     return $this->money->getAmount(); 
    } 
} 

$money = new Money(); 
$wallet = new Wallet($money); 

$wallet->addMoney(50); 
echo $wallet->getMoney(); // displays 50 
$wallet->removeMoney(20); 
echo '<br>' . $wallet->getMoney(); // displays 30 

這樣你的錢,實際上是你的錢包裏,你可以玩你想要的東西。

如果您希望直接使用Money中的函數,則必須更改Wallet類中的$money對象public。那麼你可以做這樣的東西:

class wallet { 
    public $money; // An Object 

    public function __construct(Money $money) { 
     $this->money = $money; 
    } 
} 

$money = new Money(); 
$wallet = new Wallet($money); 

$wallet->money->add(50); 
echo $wallet->money->getAmount(); // displays 50 
$wallet->money->remove(20); 
echo '<br>' . $wallet->money->getAmount(); // displays 30 
+0

您的示例已保存; – Roger

+0

public function __construct(Money $ money)我不知道如何像你一樣施放課程。現在就像一個魅力!非常感謝。對不起,我無法贊成,因爲我是新用戶 – Roger

+0

好吧,如果它能夠充分回答你的問題,你可以將其標記爲答案;) – Chris