NamespacedBankAccount.php 710 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace SomeNamespace;
  3. class BankAccount
  4. {
  5. use BankAccountTrait;
  6. }
  7. trait BankAccountTrait {
  8. protected $balance = 0;
  9. public function getBalance()
  10. {
  11. return $this->balance;
  12. }
  13. protected function setBalance($balance)
  14. {
  15. if ($balance >= 0) {
  16. $this->balance = $balance;
  17. } else {
  18. throw new \RuntimeException;
  19. }
  20. }
  21. public function depositMoney($balance)
  22. {
  23. $this->setBalance($this->getBalance() + $balance);
  24. return $this->getBalance();
  25. }
  26. public function withdrawMoney($balance)
  27. {
  28. $this->setBalance($this->getBalance() - $balance);
  29. return $this->getBalance();
  30. }
  31. }