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
}
}
So, I'm using this:
$wallet = new Wallet();
print ($wallet ->getMoney()->getSomething());
Works fine! But I have no idea if it's the correct way to access the class Money
which is inside the Wallet.
If by the field of $money
you meant the base class, then no; that's not the right way to go. You already have access to the base class in the methods of your child classes, so the field of $money
is redundant, and you should remove it.
You may initialize your Money
class using the __construct()
method, so add the following to your Money
class:
function __construct() {
$this->something = "something";
$this->another = "another";
// anything else needed to initialize this object of Money class..
}
In the wallet
class, in addition to initializing the fields directly inside the class, you need to initialize the base class like the following:
function __construct() {
parent::__construct();
// anything else needed to initialize this object of wallet class..
}
And, as the method of getSomething()
in the base class of Money
have the public
visibility, you can call it directly using $wallet->getSomething();
.
I'm not quite sure how you're getting this to work without instantiating a Money object, and you've got some typos in your code.
But anyway from your last line where you said you want to "access the money which is inside the wallet", your setup doesn't seem to make much sense. A wallet isn't a type of money, so it doesn't seem right that it is extending money.
If you want Money to be a class, you might instead use dependency injection, something like this:
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
This way your money is actually inside your wallet and you can play with it however you want.
If you'd rather use the functions in Money directly, you'd have to change the $money
object public
in the Wallet class. Then you could do stuff like this:
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