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

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;
  }
}

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.