djt1975 Posted February 27, 2015 Share Posted February 27, 2015 Please see attached my bank account class files. The key guidance I need is how to instantiate a class from another class. For example, you can see in Customer.class.php, I instantiate a new Account, but am not sure how i pass in the balance and account number to Customer in order that they can instantiate the Account? This is the same as Account instantiate a BankCard object. I am a little confused - any help appreciated. BankAccountApp.zip Link to comment https://forums.phpfreaks.com/topic/294933-oophp-instantiatiing-a-class-from-within-another-class/ Share on other sites More sharing options...
ignace Posted February 27, 2015 Share Posted February 27, 2015 Why would you instantiate a Account object inside Customer? A customer can have multiple Accounts with different banks. And Account should not instantiate a BankCard object either since not all Accounts come with a BankCard, sometimes you have to pay for them. Understanding OO is understanding how your application works. A bank screens a customer before opening an account and may decline if certain criteria aren't met (for example good credit rating). Which is also why you run into trouble passing required arguments to these classes simply because they do not belong there. class Bank { public function createNewAccountFor(Customer $customer) { $this->assertCustomerMeetsCriteria($customer); $account = new Account($this, $customer); // the bank decides how new Account's are created $account->queue(new NewAccountFee($this)); // and makes sure it applies it's own rules and those of the government $account->addEventListener('deposit', new FiftyPercentYouMakeBelongsToTheGovernmentTax()); return $account; } } Link to comment https://forums.phpfreaks.com/topic/294933-oophp-instantiatiing-a-class-from-within-another-class/#findComment-1506915 Share on other sites More sharing options...
Sanjib Sinha Posted February 27, 2015 Share Posted February 27, 2015 To learn it more please google 'type hinting'. I think it will help a lot. Link to comment https://forums.phpfreaks.com/topic/294933-oophp-instantiatiing-a-class-from-within-another-class/#findComment-1506960 Share on other sites More sharing options...
djt1975 Posted March 2, 2015 Author Share Posted March 2, 2015 Thanks. It looks like you are saying a controller class called 'Bank' needs to instantiate Account and Bank Card. I guess this makes sense. Forgive my being a little confused. I struggled with the relationsip side of things coming from a RDBMS background. I saw the "Customer 'has' Account" relationship and thought this must mean instantiates. Is there further reading you can point me to on this aspect? Link to comment https://forums.phpfreaks.com/topic/294933-oophp-instantiatiing-a-class-from-within-another-class/#findComment-1507250 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.