Skip to content

PHP Tutorial Data Types

Objects

Lesson 40 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

An object is a value that bundles data (called properties) and actions (called methods) in one package. You create objects from a blueprint called a class.

Think of a class as a cookie cutter and objects as the cookies.

index.php
PHP
<?php
class Car
{
    public string $brand = "Tata";

    public function honk(): string
    {
        return "Beep!";
    }
}

$car = new Car();

echo $car->brand . "\n";
echo $car->honk();
Output
OUTPUT
Tata
Beep!
  • class Car defines the blueprint.
  • new Car() creates one object.
  • -> reaches inside the object to read a property or call a method.

Checking an object

index.php
PHP
<?php
class Car {}
$car = new Car();

var_dump($car instanceof Car);
echo get_debug_type($car);
Output
OUTPUT
bool(true)
Car

The built-in stdClass

PHP has an empty class called stdClass for quick, plain objects. You will often meet it when decoding JSON.

index.php
PHP
<?php
$point = new stdClass();
$point->x = 10;
$point->y = 20;

echo $point->x + $point->y;
Output
OUTPUT
30

Objects are handled by reference

Assigning an object to another variable does not copy it. Both variables point to the same object.

index.php
PHP
<?php
$a = new stdClass();
$a->name = "first";

$b = $a;
$b->name = "changed";

echo $a->name;
Output
OUTPUT
changed
info
Objects get their own chapters
Object-Oriented PHP teaches classes step by step. For now, remember the -> arrow and the word new.