PHP Classes and Objects for Beginners: Simple Guide
PHP classes and objects for beginners: learn what a class is, how to use new, constructors and private properties, with simple code you can copy and run.
PHP classes and objects for beginners: learn what a class is, how to use new, constructors and private properties, with simple code you can copy and run.
PHP classes and objects let you group related data and actions into one reusable package. If you searched for "php classes and objects for beginners", here is the short answer: a class is a blueprint, and an object is a real thing built from that blueprint. In this guide you will write your first class, create objects, and use constructors and visibility, step by step.
A class is a plan that describes something. Think of the plan for a house. The plan is not a house you can live in, but you can build many houses from it. In PHP, the plan is the class. Each house you build is an object.
A class can hold two kinds of things. Properties are variables that belong to the class (the data). Methods are functions that belong to the class (the actions). For a Car class, a property could be the colour, and a method could be drive.
<?php
class Car {
public $brand;
public $color;
public function describe() {
return "This is a " . $this->color . " " . $this->brand . ".";
}
}Line 2 uses the word class and gives the blueprint a name, Car. Class names usually start with a capital letter. Lines 3 and 4 declare two properties, brand and color. The word public means any code can read and change them. Line 6 declares a method named describe. Inside it, $this means "the object I am working on right now", and the arrow -> reaches a property of that object. The dot joins pieces of text together.
<?php
$myCar = new Car();
$myCar->brand = "Toyota";
$myCar->color = "red";
echo $myCar->describe();Line 2 uses the keyword new to build an object from the Car class and stores it in $myCar. We say $myCar is an instance of Car. Lines 3 and 4 set its properties using the arrow. Line 6 calls the describe method, which prints: This is a red Toyota.
The real power is reuse. You write the class once and create as many objects as you need. Each object keeps its own values.
<?php
$car1 = new Car();
$car1->brand = "Honda";
$car1->color = "blue";
$car2 = new Car();
$car2->brand = "Ford";
$car2->color = "green";
echo $car1->describe() . "\n";
echo $car2->describe() . "\n";We build two separate objects. Changing $car2 does not change $car1. The output is two different sentences, produced by the same method.
Setting every property by hand is slow. A constructor is a special method that runs automatically when you use new. In PHP it is named __construct (two underscores).
<?php
class Car {
public $brand;
public $color;
public function __construct($brand, $color) {
$this->brand = $brand;
$this->color = $color;
}
public function describe() {
return "This is a " . $this->color . " " . $this->brand . ".";
}
}
$car = new Car("Tesla", "white");
echo $car->describe();The constructor on line 6 receives two values. Lines 7 and 8 copy them into the object's properties. On line 16, the values in brackets after new Car are passed straight to the constructor. Now one line creates a fully set-up object.
Visibility controls who can touch a property or method. The three keywords are in the table below.
| Keyword | Who can use it |
|---|---|
| public | Any code, inside or outside the class |
| private | Only code inside the same class |
| protected | The class and classes that extend it (a later topic) |
Private properties stop other code from setting bad values. You then offer public methods to change them safely.
<?php
class BankAccount {
private $balance = 0;
public function deposit($amount) {
if ($amount > 0) {
$this->balance += $amount;
}
}
public function getBalance() {
return $this->balance;
}
}
$account = new BankAccount();
$account->deposit(50);
echo $account->getBalance();The balance is private, so writing $account->balance = 1000000 from outside causes an error. The only way to add money is deposit, and line 5 refuses negative amounts. getBalance lets outside code read the value without changing it. This output is 50.
You do not need classes for tiny scripts. They help when you have things with both data and behaviour, such as users, products, or orders. Most PHP frameworks, including Laravel, are built from classes, so learning them now makes later tutorials easier.
A class is the blueprint that describes properties and methods. An object is one real instance created from that blueprint with the new keyword. You can create many objects from one class.
$this refers to the current object inside a class method. It lets the method read and change that object's own properties, for example $this->color.
A constructor is a method named __construct that runs automatically when an object is created. It is mostly used to set the starting values of properties.
A property is a variable inside a class and holds data. A method is a function inside a class and performs an action. Both are reached with the arrow ->.
You can write simple PHP without them. But classes are used in almost all modern PHP projects and frameworks, so learning the basics is a worthwhile next step after functions and arrays.
New to functions? Read our beginner guide first, since methods are just functions inside a class.
Read: PHP functions for beginnersNeed help turning your idea into a real web application? Talk to the Fepiq team.
Contact FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL CASE WHEN step by step. This beginner guide shows how to label, group and sort data with simple examples you can copy and run today.
PostgreSQL ILIKE explained for beginners: search text without worrying about case, learn % and _ wildcards, and copy simple examples you can run today.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.