CodeBlanks Logo Icon CodeBlanks

Summary - Constructor

PHP course · Intermediate · Lesson 12/115

Build a bank account class with constructor, parameters and property initialization.

Summary - Constructor

In this group you learned the constructor and destructor methods. In short:

  • __construct: called automatically when an instance is created with new. It can take parameters and give properties their initial values.
  • __destruct: called when the instance is destroyed; used to clean up external resources.
  • A class type declaration (Engine $engine) restricts a parameter to that class and enables dependency injection.

Now you'll combine all of this in a bank account class.

Exercise Write the BankAccount class

Define a class named BankAccount. Declare properties public string $owner; and public float $balance;. The constructor should take two parameters with the signature __construct(string $owner, float $balance) and perform $this->owner = $owner; and $this->balance = $balance;. Below the class create an instance with $acc = new BankAccount("Alex", 1500.0); and print it with echo $acc->owner . ": " . $acc->balance . "\n";.

Show example solution
" . $acc->balance . "\n";.
Example solution:
<?php
// Define the BankAccount class
class BankAccount {
    public string $owner;
    public float $balance;

    public function __construct(string $owner, float $balance) {
        $this->owner = $owner;
        $this->balance = $balance;
    }
}

$acc = new BankAccount("Alex", 1500.0);
echo $acc->owner . ": " . $acc->balance . "\n";

Exercise Add a destructor to BankAccount

Add a __destruct method to the previous BankAccount class. The destructor body should print echo "Account closed: " . $this->owner . "\n";. Keep the example call below the class the same: $acc = new BankAccount("Alex", 1500.0); and echo $acc->owner . ": " . $acc->balance . "\n";. The destructor runs at script end.

Show example solution
" . $this->owner . "\n"; }. Keep the example call and echo lines below the same.
Example solution:
<?php
// Define the BankAccount class, add constructor and destructor
class BankAccount {
    public string $owner;
    public float $balance;

    public function __construct(string $owner, float $balance) {
        $this->owner = $owner;
        $this->balance = $balance;
    }
    // Add the __destruct method here
    public function __destruct() {
        echo "Account closed: " . $this->owner . "\n";
    }
}

$acc = new BankAccount("Alex", 1500.0);
echo $acc->owner . ": " . $acc->balance . "\n";

Test yourself

  1. Match the concepts

    __construct
    runs at start
    __destruct
    runs at end
    Engine $engine
    class-typed parameter
    $this->balance
    instance property
  2. Which statement is true?

    • __construct returns a value with return
    • The constructor takes no parameters
    • __destruct is called during new
    • A class type declaration raises a TypeError on the wrong type
    Show answer

    A class type declaration raises a TypeError on the wrong type — Correct. A class type declaration raises a TypeError on the wrong type, which improves safety.

All lessons in Intermediate