Classes and objects

A class puts data and the functions that work on it into one named unit. For PHP this is the step from loose variables to objects that carry their own state.

Define a class

A class is a blueprint for values with data and behavior. Declare properties for data and keep names clear and short.

Full syntax is covered in Classes in PHP 8 with more property examples.

<?php class User { public string $name = "Guest"; } $user = new User(); echo $user->name, PHP_EOL;

php class.php

Guest

The property carries its default, so a fresh object answers with Guest before anything assigns another name. The type string in front of the property keeps that promise.

Create objects

Use new to build an object from a class. Each object holds its own copy of property values.

Access properties with the arrow operator and assign new values the same way.

<?php class User { public string $name = "Guest"; } $user = new User(); $user->name = "Ada"; echo $user->name, PHP_EOL;

php new.php

Ada

The new name replaces the default in this one object only; a second new User() would start again from Guest, because the default belongs to the class and the value to the object.

Methods

Methods are functions declared inside a class. They group behavior next to the data it uses.

Declare return types so readers know what each method gives back.

<?php class Greeter { public function hello(string $name): string { return "Hello " . $name; } } $greeter = new Greeter(); echo $greeter->hello("Ada"), PHP_EOL;

php methods.php

Hello Ada

The method is called on the object with the same arrow that reads a property, and the parentheses after its name are what turn it into a call.

What $this is

Inside a method, $this points to the current object. Use it to read and update properties of that same object.

Each call sees its own values, so two objects never share state through $this.

<?php class Counter { public int $total = 0; public function add(int $step): void { $this->total = $this->total + $step; } } $first = new Counter(); $first->add(3); $first->add(4); echo $first->total, PHP_EOL; $second = new Counter(); $second->add(1); echo $second->total, PHP_EOL;

php this.php

7
1

The first object collected 3 and 4, and the second one started again from zero, so the property of one object is no property of the other. The method could not tell them apart without $this.

Exercises

  1. Change the default of $name in class.php to "nobody" and run it. Which line changes?
  2. Create two users in new.php, give them different names and print both. Do the objects share the property?
  3. Add a method farewell() to Greeter and call it on the object.
  4. Give Counter a method reset() that sets the total back to zero, then add, reset and print.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is a class?

    Answer

    A blueprint that describes the data and methods of its objects

    The class holds the description; every object built from it gets its own properties and shares the methods.

  2. What does new do?

    Answer

    It builds an object from a class

    The new keyword creates an instance of the named class and returns it.

  3. Which operator reads a property or calls a method?

    Answer

    The arrow ->

    The arrow works on an object; the double colon reaches static members of a class.

  4. What does $this refer to inside a method?

    Answer

    The object the method was called on

    Each call binds $this to its own object, which is how two counters keep separate totals.

  5. What does this program print? <?php class User { public string $name = "Guest"; } $user = new User(); echo $user->name;

    Answer

    Guest

    The property has a default in the class, and the fresh object answers with it.

  6. What does this program print? <?php class User { public string $name = "Guest"; } $user = new User(); $user->name = "Ada"; echo $user->name;

    Answer

    Ada

    The assignment replaces the default in that object, and the echo reads the new value.

  7. What does this program print? <?php class Greeter { public function hello(string $name): string { return "Hello " . $name; } } $greeter = new Greeter(); echo $greeter->hello("Ada");

    Answer

    Hello Ada

    The method returns the greeting built from its argument, and echo prints the returned string.

  8. What does this program print? <?php class Counter { public int $n = 0; public function add(): void { $this->n++; } } $a = new Counter(); $a->add(); $a->add(); $b = new Counter(); $b->add(); echo $a->n . $b->n;

    Answer

    21

    The first object counts two calls and the second one counts one, and the two numbers print side by side.

  9. What does this program print? <?php class Point { public int $x = 5; } $point = new Point(); echo $point->x;

    Answer

    5

    The property holds its default, and the arrow reads it from the object.

  10. What does a second object of the same class start with?

    Answer

    The default values declared in the class

    Every object begins from the class defaults, so the two counters of the example start at zero independently.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev