intermediate20 min

Classes and Objects

Defining a PHP class with typed properties and methods, and creating instances.

What you'll learn

  • Define a class with typed properties, a constructor, and methods
  • Explain what `$this` refers to inside a method
  • Predict the output of code that creates and mutates an object

Explanation

A PHP class bundles typed properties and methods together: class BankAccount { private float $balance; } declares a property, and public function __construct(float $startingBalance) { $this->balance = $startingBalance; } is the special constructor method, automatically called when you create an instance with new BankAccount(100).

Inside any method, $this refers to the specific object the method was called on -- $this->balance reads or writes that instance's balance property, using -> (not .) to access properties and methods on an object.

Visibility keywords (public, private, protected) control where a property or method can be accessed from: private means only code inside the same class can touch it directly, which is why BankAccount exposes a getBalance() method rather than letting outside code read $balance directly.

Methods, like standalone functions, can declare parameter and return types: public function deposit(float $amount): void takes a float and returns nothing meaningful (void), while public function getBalance(): float returns the current balance as a float.

Guided lab

Predict: A class with a constructor and methods

PHPNot executed
This lab does not run in your browser or on VisaSparkSchools's servers. Read the code, predict what it does, then reveal the real expected output.

Read this script and predict exactly what it sends to the browser.

<?php
class BankAccount {
    private float $balance;

    public function __construct(float $startingBalance) {
        $this->balance = $startingBalance;
    }

    public function deposit(float $amount): void {
        $this->balance += $amount;
    }

    public function getBalance(): float {
        return $this->balance;
    }
}

$account = new BankAccount(100);
$account->deposit(50);
echo "Balance: " . $account->getBalance() . "\n";

Stuck? Get a hint.

Common mistakes

  • Using `.` instead of `->` to access a property or call a method on an object -- `.` is PHP's string concatenation operator, not member access.
  • Trying to read a `private` property directly from outside the class (`$account->balance`) instead of through a public method like `getBalance()`.
  • Forgetting the constructor runs automatically on `new ClassName(...)` -- you never call `__construct()` yourself.

Knowledge check

Knowledge check

1. What operator does PHP use to access a property or method on an object?
2. What does `$this` refer to inside an instance method?
3. Why does `BankAccount` provide a `getBalance()` method instead of a public `$balance` property?

Takeaway

Use `->` to access properties/methods on an object, and keep mutable state `private`, exposing it only through methods you control.

Summary

A class bundles typed properties and methods; `new ClassName(...)` calls the constructor automatically; `$this` refers to the current instance, accessed via `->`.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.