Classes, Objects and Methods
💻 PHPGroup related data and behaviour into classes, and create reusable objects.
A class is a blueprint. An object is one thing built from that blueprint. Classes keep related data and the functions that work on it in one place.
- Define a class with properties and methods.
- Create objects with new.
- Use constructors and visibility.
Your First Class
A class holds properties (data) and methods (functions). Use $this inside a method to reach the current object.
<?php
class Student
{
public string $name;
public int $score = 0;
public function addScore(int $points): void
{
$this->score += $points;
}
public function grade(): string
{
return match (true) {
$this->score >= 70 => 'A',
$this->score >= 60 => 'B',
$this->score >= 50 => 'C',
default => 'F',
};
}
}
$student = new Student();
$student->name = 'Amina';
$student->addScore(72);
echo $student->name . ' scored ' . $student->grade();Constructors
A constructor runs when the object is created. It is the right place to require the values an object needs to be valid.
<?php
class Student
{
public function __construct(
private string $name,
private int $score = 0,
) {
}
public function name(): string
{
return $this->name;
}
}
$student = new Student('Bola', 65);
echo $student->name();Visibility
Use public for things outside code may use, private for internal details, and protected for subclasses. Prefer private properties with public methods so the object controls its own state.
Practice
- Create a
BankAccountclass withdepositandwithdrawmethods. - Stop a withdrawal that is larger than the balance.
- Add a constructor that requires the account holder name.
Hands-on exercise
Create a class named Product.
1. It takes a name and a price in the constructor.
2. It has a method priceWithVat() that returns price × 1.075, rounded to two decimals.
3. Create an object and print the result.
Recap
Classes group data and behaviour. Constructors set up valid objects, and visibility protects internal state.
Example
<?php
class Student
{
public string $name;
public int $score = 0;
public function addScore(int $points): void
{
$this->score += $points;
}
public function grade(): string
{
return match (true) {
$this->score >= 70 => 'A',
$this->score >= 60 => 'B',
$this->score >= 50 => 'C',
default => 'F',
};
}
}
$student = new Student();
$student->name = 'Amina';
$student->addScore(72);
echo $student->name . ' scored ' . $student->grade();
Exercises
1. It takes a name and a price in the constructor.
2. It has a method priceWithVat() that returns price × 1.075, rounded to two decimals.
3. Create an object and print the result.
Expected Output
2687.5
Lesson Nav
PHP Course
0/9 lessons completed
- 1 PHP Basics Tutorial
- 2 Functions and Arrays Tutorial
- 3 PHP Forms and User Input Tutorial
- 4 PHP and MySQL Database Tutorial
- 5 Build a Blog System Project
- 6 Classes, Objects and Methods Tutorial
- 7 Validation and Secure Input Tutorial
- 8 Working with JSON and APIs Tutorial
- 9 Project: A Small JSON API Project