All posts
PHPOctober 20, 20267 min read

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.

F
Fepiq Team
Fepiq

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.

What is a class in PHP?

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.

Your first PHP class

php
<?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.

Creating an object with new

php
<?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.

Many objects from one class

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
<?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.

Constructors: set values when you create an object

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
<?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.

Public and private: protecting your data

Visibility controls who can touch a property or method. The three keywords are in the table below.

KeywordWho can use it
publicAny code, inside or outside the class
privateOnly code inside the same class
protectedThe 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
<?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.

Common beginner mistakes

  • Forgetting $this-> inside a class, so PHP looks for a plain variable instead of the property.
  • Writing a dollar sign when reading a property: use $car->brand, not $car->$brand.
  • Forgetting new, so no object is created.
  • Making everything public. Prefer private unless outside code really needs access.
  • Misspelling __construct. It needs two underscores at the start.

When should you use classes?

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.

Frequently asked questions

What is the difference between a class and an object in PHP?+

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.

What does $this mean in PHP?+

$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.

What is a constructor in PHP?+

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.

What is the difference between a property and a method?+

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 ->.

Do I need to learn classes to use PHP?+

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 beginners

Need help turning your idea into a real web application? Talk to the Fepiq team.

Contact Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.