OOP Basics

Course 2 · Ch 1
OOP Basics: Classes, Objects, Properties, and Methods
Moving from standalone functions and arrays to bundling data and behaviour together

Throughout PHP Fundamentals, data (variables, arrays) and behaviour (functions) were kept separate — a function received whatever data it needed as parameters. Object-Oriented Programming (OOP) groups related data and the functions that operate on it into a single unit called an object. This chapter introduces the core vocabulary: classes, objects, properties, and methods.

Defining a Class

<?php class Person { public string $name; public int $age; public function greet() { echo "Hi, I'm {$this->name}!"; } } ?>

A class is a blueprint — it describes what kind of data a thing has (properties: $name, $age) and what it can do (methods: greet()). No actual person exists yet — the class only defines the shape.

Creating Objects — Instances of a Class

<?php $person1 = new Person(); $person1->name = "Philip"; $person1->age = 35; $person2 = new Person(); $person2->name = "Sam"; $person2->age = 28; $person1->greet(); // "Hi, I'm Philip!" $person2->greet(); // "Hi, I'm Sam!" ?>

new Person() creates an object (also called an "instance") from the Person blueprint. Each object has its own independent copy of the properties — changing $person1->age has no effect on $person2, even though both came from the same class.

class Person (blueprint) properties: $name, $age methods: greet() $person1 "Philip", 35 $person2 "Sam", 28
One class, many independent objects — each with its own property values

$this — Referring to "the Current Object"

$this only makes sense inside a method, and refers to whichever object called it
Inside greet(), $this->name means "the $name property belonging to whichever object this method was called on." When $person1->greet() runs, $this refers to $person1; when $person2->greet() runs, $this refers to $person2 instead — the exact same method code, but it acts on different data each time.

The Constructor — Setting Up an Object on Creation

<?php class Person { public string $name; public int $age; public function __construct(string $name, int $age) { $this->name = $name; $this->age = $age; } public function greet() { echo "Hi, I'm {$this->name}!"; } } $person1 = new Person("Philip", 35); // properties set immediately, no separate assignment lines needed $person1->greet(); ?>

__construct() is a special method PHP runs automatically every time new ClassName(...) is called. It removes the need to set every property manually on a separate line after creating the object — and, crucially, it can require certain values to always be provided up front (a constructor with required parameters cannot be skipped).

Constructor property promotion — a shorter, very common modern shorthand
public function __construct(public string $name, public int $age) {} — adding the visibility keyword directly in the constructor's parameter list automatically creates and assigns the property, removing the need to declare it separately above and write the $this->name = $name; line. Both styles are shown across real PHP code; this course uses the explicit version above for clarity while the concept is still new.

public, private, and protected — Controlling Access

<?php class BankAccount { private float $balance = 0; // cannot be accessed directly from outside this class public function deposit(float $amount) { $this->balance += $amount; } public function getBalance(): float { return $this->balance; } } $account = new BankAccount(); $account->deposit(100); echo $account->getBalance(); // 100 // $account->balance = 999999; // ERROR — balance is private, cannot be set directly from outside ?>

private properties can only be read or changed by code inside the same class — forcing any change to go through controlled methods like deposit(), rather than letting outside code set $balance to anything at all. public means accessible from anywhere; protected (covered fully in Chapter 2 with inheritance) sits in between.

This deliberate restriction is called encapsulation, and it's one of OOP's central ideas
Without private, nothing would stop other code from setting a bank balance to a negative number, or any arbitrary value, bypassing whatever rules deposit() was meant to enforce. Making the property private and only exposing it through methods means the class itself can guarantee its own data stays valid.

Coding Challenges

Challenge 1

Create a Book class with public properties $title, $author, and a constructor that sets both. Add a method describe() that echoes "[title] by [author]". Create two different Book objects and call describe() on each.

📄 View solution
Challenge 2

Create a Counter class with a private property $count starting at 0, and public methods increment() (adds 1), decrement() (subtracts 1), and getCount(). Create one Counter, call increment() three times and decrement() once, then echo the result via getCount().

📄 View solution
Challenge 3

Create a Rectangle class with a constructor taking $width and $height, and methods getArea() and getPerimeter(). Create two rectangles with different dimensions and echo both calculated values for each, demonstrating they're independent of one another.

📄 View solution

Chapter 1 Quick Reference

  • class Name { } — a blueprint defining properties (data) and methods (behaviour)
  • new ClassName() — creates an object (instance) from the class blueprint
  • $this — inside a method, refers to whichever object the method was called on
  • __construct() — special method run automatically when an object is created via new
  • public / private / protected — control where a property/method can be accessed from
  • Encapsulation — using private + controlled methods to keep an object's data valid
  • Next chapter: OOP in depth — inheritance, interfaces, abstract classes, traits