PHP Object-Oriented Programming (OOP)

Key Takeaway: Practice PHP programming with 84+ practical code examples covering syntax, arrays, strings, functions, OOP, JSON, and file handling — all with expected terminal outputs.

Audience & Use Case: Designed for web developers, backend engineers, Laravel developers, computer science students, and interview candidates mastering modern PHP.

Last updated: September 2026 84 Verified Examples

Quick Start & How to Use

  1. Select a PHP program topic from the sidebar menu (or mobile dropdown).
  2. Examine the clear PHP code syntax and corresponding terminal output.
  3. Copy the code directly or run it live in our Online PHP Compiler.
  4. Experiment with arrays, functions, and OOP classes to solidify your server-side programming skills.

Who is this for: Web developers, Laravel learners, API engineers, and CS students.

Related Tools:PHP Compiler · PHP Official Manual · JavaScript Examples · Python Examples

How to use Codeignitor (PHP)?

Run Online

Overview and sample Controller/Model architecture structure for CodeIgniter framework.

Program (PHP)
<?php

namespace App\Controllers;

class Home {
    public function index() {
        $data = ['title' => 'Welcome to CodeIgniter 4'];
        return "Controller Response: " . $data['title'];
    }
}

$controller = new Home();
echo $controller->index() . "\n";
?>
Expected Output
Controller Response: Welcome to CodeIgniter 4

PHP Object-Oriented Programming (Classes & Objects)

Run Online

Defines a PHP class with properties, constructor, and methods.

Program (PHP)
<?php

class Car {
    public $brand;
    public $model;

    public function __construct($brand, $model) {
        $this->brand = $brand;
        $this->model = $model;
    }

    public function getDetails() {
        return "Car: {$this->brand} {$this->model}";
    }
}

$myCar = new Car("Toyota", "Corolla");
echo $myCar->getDetails() . "\n";
?>
Expected Output
Car: Toyota Corolla

PHP Class Inheritance Example

Run Online

Illustrates OOP inheritance using extends keyword and protected properties.

Program (PHP)
<?php

class Animal {
    protected $name;

    public function __construct($name) {
        $this->name = $name;
    }
}

class Dog extends Animal {
    public function bark() {
        return "{$this->name} says: Woof Woof!";
    }
}

$dog = new Dog("Buddy");
echo $dog->bark() . "\n";
?>
Expected Output
Buddy says: Woof Woof!

How to build a Grocery Store Web App using PHP with MySQL?

Run Online

Architecture and CRUD implementation for a PHP grocery store application.

Program (PHP)
<?php

class GroceryStore {
    private $products = [
        1 => ['name' => 'Organic Apples', 'price' => 2.99, 'stock' => 50],
        2 => ['name' => 'Whole Milk 1L', 'price' => 1.49, 'stock' => 30],
        3 => ['name' => 'Whole Wheat Bread', 'price' => 2.29, 'stock' => 20]
    ];

    public function getProducts() {
        return $this->products;
    }

    public function addToCart($productId, $quantity) {
        if (isset($this->products[$productId])) {
            $item = $this->products[$productId];
            $total = $item['price'] * $quantity;
            return "Added {$quantity}x {$item['name']} to cart. Total: \${$total}";
        }
        return "Product not found.";
    }
}

$store = new GroceryStore();
echo $store->addToCart(1, 3) . "\n";
?>
Expected Output
Added 3x Organic Apples to cart. Total: $8.97

PHP Core Concepts & Feature Breakdown

DomainCore ConceptsPrimary Use CaseOfficial Docs
Variables & Types$var, string, int, boolVariables, string concatenation, type declarationsPHP Types
Arrays & DataIndexed arrays, Associative ['key' => 'val']Key-value mappings, list iteration with foreachPHP Arrays
Functionsfunction name(type $param): returnTypeModular code, default arguments, return value typingPHP Functions
OOP Constructsclass, extends, public/protectedEncapsulation, constructors, inheritance, web frameworksPHP OOP

Keep Practicing

Use the online PHP compiler to test snippet modifications, test array manipulation logic, and build custom functions for backend applications.

Frequently Asked Questions

You can run all snippets instantly in your browser using our Online PHP Compiler without setting up Apache, Nginx, or local PHP runtimes.

Yes. All programs utilize modern PHP standard syntax, typed properties/arguments, short array syntax [], and standard OOP paradigms compatible with PHP 7.4 through PHP 8.x and frameworks such as Laravel and Symfony.

Absolutely. These examples cover essential backend concepts frequently requested during technical evaluations: string operations, prime checks, Fibonacci algorithms, associative array iteration, JSON parsing, and object-oriented inheritance.

Learn PHP by Practicing Examples

PHP powers over 75% of the active web today. Practicing short, clear programs is the most efficient path to mastering PHP backend development. Each snippet focuses on a fundamental building block — from basic variable output and control structures to complex associative arrays, JSON serialization, and object-oriented design.

Beginner Foundations

Learn echo syntax, variable assignment, type coercion, and simple arithmetic operations.

Arrays & Logic

Master key-value arrays, foreach loops, function type hints, and string/math helpers.

OOP & Files

Explore classes, inheritance, visibility, JSON APIs, and file read/write operations.