Created
February 22, 2019 22:48
-
-
Save ozumoo/c2bd87e5294d4149649bfddae0a1295c to your computer and use it in GitHub Desktop.
indentation in php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //Bank Accounts class | |
| class BankAccounts | |
| { | |
| //constructor | |
| protected $accounts; | |
| function __construct($accounts) | |
| { | |
| $this->accounts = $accounts; | |
| } | |
| //filter method | |
| public function filterBy($accountType) | |
| { | |
| return array_filter($this->accounts , function($account) use ($accountType) | |
| { | |
| return $account->isOfType($accountType) ; | |
| }); | |
| } | |
| } | |
| //Account class | |
| class Account | |
| { | |
| protected $type; | |
| private function __construct($type) | |
| { | |
| $this->type = $type; | |
| } | |
| // as we open a bank account | |
| public static function open($type) | |
| { | |
| return new static($type) ; | |
| } | |
| public function isOfType($accountType) | |
| { | |
| return $this->type() == $accountType && $this->isActive(); | |
| } | |
| private function type() | |
| { | |
| return $this->type ; | |
| } | |
| private function isActive() | |
| { | |
| return true; | |
| } | |
| } | |
| $accounts = [ | |
| Account::open('checking'), | |
| Account::open('savings'), | |
| Account::open('checking'), | |
| Account::open('savings') | |
| ]; | |
| $accounts = new BankAccounts($accounts); | |
| $savings = $accounts->filterBy('savings'); | |
| var_dump($savings); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment