PHPPHP18 min read

OOP in PHP: Classes and Objects

Understand OOP fundamentals in PHP with real examples: classes, objects, properties, and methods.

Jacob Wilson
October 22, 2025
6.6k238

OOP (Object-Oriented Programming) helps you organize code in a clean, reusable structure. Modern PHP projects and frameworks (Laravel, Symfony) use OOP heavily.

Class vs Object

  • Class = blueprint
  • Object = real instance made from that blueprint

Example class

<?php
class User {
  public string $name;
  public string $email;

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

  public function greet(): string {
    return "Hello, I am " . $this->name;
  }
}

$user = new User("Emma Parker", "emma@test.com");
echo $user->greet();
?>

Why OOP is useful

  • code is easier to reuse
  • you can model real-world entities (User, Post, Order)
  • easier testing and maintenance

Next: Constructors, visibility (public/private), and best practices.

#PHP#OOP#Intermediate