Jump to content

OOPHP - instantiatiing a class from within another class


djt1975

Recommended Posts

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
Share on other sites

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;
  }
}
Edited by ignace
  • Like 1
Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.