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

PHP OOP Introduction

Object-oriented programming builds programs from things called objects, like a toy box full of robots that each know their own facts and moves. It keeps big programs neat by grouping data with what it can do.
Syntax
php
class ClassName {
    public $property;

    function method_name() {
        // uses $this->property
    }
}

$object = new ClassName();
$object->method_name();

What is Object-Oriented Programming?

Object-oriented programming organizes code around objects that bundle data (properties) and behavior (methods) together, instead of scattering related variables and functions loosely across a script.

Example: What is Object-Oriented Programming?

php
<?php
// Define the class `Car`
class Car {
    // Declare the public property `$color`, set to "red"
    public $color = "red";
    // Define the function `drive` with no parameters
    function drive() {
        // Print "Driving a $this->color car" to the output
        echo "Driving a $this->color car";
    }
}
// Create a new `Car` instance, stored in `$car`
$car = new Car();
$car->drive();
?>

Benefits of OOP

A class is a blueprint that defines what properties and methods its objects will have, while an object is one concrete instance created from that blueprint — much like a house blueprint versus an actual house built from it.

Example: Benefits of OOP

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 = "blue";
// Print `$myCar->color` to the output
echo $myCar->color;
?>

Classes vs Objects

PHP has supported OOP since PHP 4 and expanded it heavily in PHP 5 and later, so modern PHP frameworks like Laravel and Symfony are built almost entirely around classes, objects, and the patterns that come with them.

Example: Classes vs Objects

php
<?php
// Define the class `Product`
class Product {
    // Declare the public property `$name`
    public $name;
}
// Create a new `Product` instance, stored in `$p`
$p = new Product();
$p->name = "Laptop";
// Print `$p->name` to the output
echo $p->name;
?>

Properties in OOP

OOP encourages reusability and organization: once you define a Product class, you can create as many product objects as you need, each with its own data but sharing the same behavior defined once in the class.

Example: Properties in OOP

php
<?php
// Define the class `Product`
class Product {
    // Declare the public property `$name`
    public $name;
    // Declare the public property `$price`
    public $price;
}
// Create a new `Product` instance, stored in `$p1`
$p1 = new Product();
$p1->name = "Laptop";
// Create a new `Product` instance, stored in `$p2`
$p2 = new Product();
$p2->name = "Phone";
// Print `$p1->name . " / " . $p2->name` to the output
echo $p1->name . " / " . $p2->name;
?>

Methods in OOP

The core OOP pillars — encapsulation, inheritance, polymorphism, and abstraction — each solve a different organizational problem, and later sections in this course build up each one individually with concrete PHP examples.

Example: Methods in OOP

php
<?php
// Define the class `Product`
class Product {
    // Declare the public property `$name`, set to "Book"
    public $name = "Book";
    // Define the function `describe` with no parameters
    function describe() {
        // Print `"This product is: " . $this->name` to the output
        echo "This product is: " . $this->name;
    }
}
// Create a new `Product` instance, stored in `$p`
$p = new Product();
$p->describe();
?>
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. Confusing a class (the blueprint) with an object (an instance created with new).
  2. Forgetting $this-> when using a property inside a method, which creates an unrelated local variable.
  3. Making every property public, which lets any code change the internal state.

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.