PHP OOP Introduction

11/22/2025

PHP OOP Introduction with OOP examples

Go Back

PHP OOP Introduction: Understanding Object-Oriented Programming in PHP

Object-Oriented Programming (OOP) is a method of structuring code to make it modular, reusable, and scalable. Modern PHP (especially PHP 7 and PHP 8) fully supports OOP features such as classes, objects, inheritance, polymorphism, interfaces, and traits.

This tutorial gives a beginner‑friendly introduction to PHP OOP with clear explanations and developer examples.


PHP OOP Introduction with OOP examples

🔹 What Is Object‑Oriented Programming?

OOP organizes code around objects rather than functions. Objects are instances of classes, and they bundle data (properties) and behavior (methods) together.

Benefits of OOP:

  • Reusability

  • Cleaner and structured code

  • Easy to maintain and extend

  • Ideal for large applications

  • Encapsulation of logic


🔸 1. Classes and Objects

A class is a blueprint.
An object is an instance of a class.

Example: Creating a Class and Object

<?php
class Car {
    public $brand = "Tata";
    public $color = "White";

    public function startEngine() {
        echo "Engine started";
    }
}

$myCar = new Car();

echo $myCar->brand;      // Output: Tata
$myCar->startEngine();   // Output: Engine started
?>

🔸 2. Properties and Methods

  • Properties = variables inside a class

  • Methods = functions inside a class

class Student {
    public $name;
    public $age;

    public function introduce() {
        echo "My name is " . $this->name;
    }
}

$st = new Student();
$st->name = "Shubham";
$st->introduce();

🔸 3. Constructors (__construct)

A constructor runs automatically when an object is created.

class User {
    public function __construct($name) {
        echo "User created: $name";
    }
}

$u = new User("Amit");

🔸 4. Inheritance

A class can inherit properties and methods from another class using the extends keyword.

class Animal {
    public function sound() {
        echo "Animal makes sound";
    }
}

class Dog extends Animal {
    public function bark() {
        echo "Dog barks";
    }
}

d = new Dog();
$d->sound();   // From parent
$d->bark();    // Child

🔸 5. Encapsulation (Public, Private, Protected)

Access modifiers control visibility.

public – accessible everywhere

private – only inside the class

protected – inside class + child class

class BankAccount {
    private $balance = 0;

    public function deposit($amount) {
        $this->balance += $amount;
    }

    public function getBalance() {
        return $this->balance;
    }
}

$acc = new BankAccount();
$acc->deposit(500);
echo $acc->getBalance();

🔸 6. Polymorphism

Methods behave differently depending on the object.

class Shape {
    public function draw() {
        echo "Drawing shape";
    }
}

class Circle extends Shape {
    public function draw() {
        echo "Drawing circle";
    }
}

$shapes = [new Shape(), new Circle()];

foreach ($shapes as $s) {
    $s->draw();
}

🔸 7. Interfaces

Interfaces define method signatures but no implementation.

interface Logger {
    public function log($message);
}

class FileLogger implements Logger {
    public function log($message) {
        echo "Logged to file: $message";
    }
}

Interfaces help enforce structure in large applications.


🔸 8. Traits (PHP‑specific OOP Feature)

Traits allow code reuse across multiple classes.

trait Greeting {
    public function sayHello() {
        echo "Hello!";
    }
}

class User {
    use Greeting;
}

$u = new User();
$u->sayHello();

🔸 9. Real‑World Example: User Management Class

class User {
    private $name;
    private $email;

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

    public function getInfo() {
        return $this->name . " (" . $this->email . ")";
    }
}

$u = new User("Shubham", "shubham@example.com");
echo $u->getInfo();

Summary

You learned the basics of PHP OOP:

  • Classes & objects

  • Properties & methods

  • Constructors

  • Inheritance & polymorphism

  • Encapsulation (private/protected/public)

  • Interfaces and traits

These concepts form the foundation of modern PHP frameworks like Laravel, Symfony, and CodeIgniter.

Table of content

  • Introduction to Apache Hive
  • Hive Architecture and Components
  • Hive Modes
  • Installation and Setup
    • Installing Hive on Linux/Windows
    • Configuring Hive with Hadoop
    • Verifying the Installation
  • Working with Hive Tables
    • Internal (Managed) Tables
    • External Tables
    • Creating Tables in Hive
    • Altering and Dropping Tables
    • Partitioning in Hive
    • Bucketing in Hive
  • HiveQL Basics
    • SELECT Queries
    • Filtering Data with WHERE
    • Sorting and Grouping Data
    • Using Joins in Hive
  • Advanced Hive Concepts
    • Partition Pruning
    • Dynamic Partitioning
    • Query Optimization in Hive
    • Working with Hive Indexes
    • ACID Transactions in Hive
  • File Formats in Hive
    • Text File
    • ORC (Optimized Row Columnar)
    • Parquet
    • Avro
    • Sequence File
  • Hive Functions
    • Built-in Functions (String, Date, Math)
    • Aggregate Functions
    • User-Defined Functions (UDFs)
  • Integrating Hive with Other Tools
    • Hive and Apache Spark
    • Hive and Pig
    • Hive and HBase
  • Hive Interview Questions
  • Best Practices in Hive
    • Performance Optimization
    • Handling Large Datasets
    • Security and Access Control
  • FAQs and Common Errors in Hive
    • Troubleshooting Hive Issues
    • Frequently Asked Questions
  • Resources and References
    • Official Hive Documentation
    • Recommended Books and Tutorials