← Back to PHP Course | Chapter 8: OOP | Lesson 2 of 14

PHP Classes & Objects

A class is like a cookie cutter and an object is a cookie made with it. You define the shape once, and then you can stamp out as many cookies as you want.
Syntax
php
class ClassName {
    public $property;

    function method_name() { }
}

$object = new ClassName();
$object->property = value;   // set a property
$object->method_name();       // call a method

Creating a Class

You define a class with the class keyword followed by a name and a body containing properties (variables) and methods (functions) that belong to it, like class Car { public $color; function drive() {} }.

Example: Creating a Class

php
<?php
// Define the class `Car`
class Car {
    // Declare the public property `$color`
    public $color;
    // Define the function `drive` with no parameters
    function drive() {
        // Print "Driving!" to the output
        echo "Driving!";
    }
}
?>

Instantiating Objects

You create an object from a class using the new keyword, such as $myCar = new Car();, which allocates a fresh instance with its own independent copy of the class's properties.

Example: Instantiating Objects

php
<?php
// Define the class `Car`
class Car {
    // Declare the public property `$color`
    public $color;
}
// Create a new `Car` instance, stored in `$myCar`
$myCar = new Car();
// Print a detailed dump (with types) of `$myCar`
var_dump($myCar);
?>

Accessing and Modifying Properties

Object properties are accessed with the -> (arrow) operator, so $myCar->color = red; sets that specific object's color without affecting any other Car object you might have created.

Example: Accessing and Modifying Properties

php
<?php
// Define the class `Car`
class Car {
    // Declare the public property `$color`
    public $color;
}
// Create a new `Car` instance, stored in `$myCar`
$myCar = new Car();
$myCar->color = 'red';
// Print `$myCar->color` to the output
echo $myCar->color;
?>

Writing Class Methods

You can create multiple independent objects from the same class, and changes to one object's properties never affect another object's properties, since each holds its own separate state in memory.

Example: Writing Class Methods

php
<?php
// Define the class `Car`
class Car {
    // Declare the public property `$color`
    public $color;
}
// Create a new `Car` instance, stored in `$car1`
$car1 = new Car();
$car1->color = "red";
// Create a new `Car` instance, stored in `$car2`
$car2 = new Car();
$car2->color = "blue";
// Print `$car1->color . " " . $car2->color` to the output
echo $car1->color . " " . $car2->color;
?>

Using the $this Keyword

Methods defined inside a class can read and modify that object's own properties directly by name, without needing to pass them in as parameters, since they execute in the context of that specific object.

Example: Using the $this Keyword

php
<?php
// Define the class `Car`
class Car {
    // Declare the public property `$color`, set to "red"
    public $color = "red";
    // Define the function `showColor` with no parameters
    function showColor() {
        // Print `$this->color` to the output
        echo $this->color;
    }
}
// Create a new `Car` instance, stored in `$car`
$car = new Car();
$car->showColor();
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting new when creating an object, such as $p = Person();, which calls an undefined function.
  2. Using -> to access a static member or :: to access an instance member.
  3. Writing $name instead of $this->name inside a method.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.