×

php · level 3 · unit 1 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A is a blueprint. An object is one thing built from it.

You have used objects such as a PDO database link and called their methods with ->. Now you design your own. A class describes a kind of thing, like a pet: the data each one holds and what it can do. Writing new Pet() builds an object from that blueprint.

Each piece of data is a , a variable living inside the object. Each action is a method, written inside the class like a function. You reach both with ->, so $rex->name is Rex's name and $rex->speak() runs his method.

Inside a method, means the object the method was called on. Two pets share one blueprint, but never their data.

<?php
class Pet {
    public string $name = "";
    public string $sound = "";

    public function speak(): string {
        return "$this->name says $this->sound!";
    }
}
$rex = new Pet();
$rex->name = "Rex";
$rex->sound = "woof";
echo $rex->speak() . "\n";

Write public before each property and method for now, and you will meet its opposite in two lessons' time.