Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
FAQs Support
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

PHP OOP Basics

Object-Oriented Programming (OOP) organizes code into classes and objects. A class is a blueprint; an object is an instance of that class.

Classes, Objects, and Constructors

Use class to define a class, new to create an object, and __construct() as the constructor. Access members with -> and use $this to refer to the current object.

Class and Object
<?php
class Car {
    // Properties
    public string $make;
    public string $model;
    private int $speed = 0;

    // Constructor
    public function __construct(string $make, string $model) {
        $this->make  = $make;
        $this->model = $model;
    }

    // Methods
    public function accelerate(int $amount): void {
        $this->speed += $amount;
    }

    public function getSpeed(): int {
        return $this->speed;
    }

    // Destructor
    public function __destruct() {
        echo "{$this->make} {$this->model} destroyed\n";
    }

    // __toString magic method
    public function __toString(): string {
        return "{$this->make} {$this->model} @ {$this->speed} km/h";
    }
}

$car = new Car("Toyota", "Corolla");
$car->accelerate(60);
echo $car->getSpeed(); // 60
echo $car;             // Toyota Corolla @ 60 km/h
?>

Access Modifiers

public — accessible everywhere. protected — accessible within the class and subclasses. private — accessible only within the class.

Access Modifiers
<?php
class BankAccount {
    public string $owner;
    protected float $balance;
    private string $pin;

    public function __construct(string $owner, float $balance, string $pin) {
        $this->owner   = $owner;
        $this->balance = $balance;
        $this->pin     = $pin;
    }

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

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

    private function validatePin(string $pin): bool {
        return $this->pin === $pin;
    }

    public function withdraw(float $amount, string $pin): bool {
        if ($this->validatePin($pin) && $this->balance >= $amount) {
            $this->balance -= $amount;
            return true;
        }
        return false;
    }
}

$acc = new BankAccount("Alice", 1000.0, "1234");
$acc->deposit(500);
echo $acc->getBalance(); // 1500
echo $acc->withdraw(200, "1234") ? "Success" : "Failed"; // Success
?>

Static Properties and Methods

Static members belong to the class itself, not to any instance. Access them with :: (scope resolution operator).

Static Members
<?php
class Counter {
    private static int $count = 0;
    const VERSION = "1.0";

    public function __construct() {
        self::$count++;
    }

    public static function getCount(): int {
        return self::$count;
    }

    public static function reset(): void {
        self::$count = 0;
    }
}

$a = new Counter();
$b = new Counter();
$c = new Counter();

echo Counter::getCount();  // 3
echo Counter::VERSION;     // 1.0
Counter::reset();
echo Counter::getCount();  // 0
?>

Key Takeaways
  • OOP in PHP uses classes (blueprints) and objects (instances). Define with class ClassName {}.
  • __construct() is the constructor — runs automatically when an object is created with new.
  • Access modifiers: public (anywhere), protected (class + subclasses), private (class only).
  • $this refers to the current object instance inside a class method.
  • Static properties/methods belong to the class, not instances — access with ClassName::method().
  • Magic methods: __toString(), __get(), __set(), __call(), __destruct().
  • PHP 8+ constructor promotion: public function __construct(public string $name, private int $age) {}
Common Mistakes to Avoid
WRONG class User { public $password; }
RIGHT class User { private string $password; }
Always make sensitive properties private. Expose them only through controlled getter/setter methods.
WRONG $obj->method = function(){};
RIGHT Define methods inside the class body
PHP does not support adding methods to objects at runtime like JavaScript. All methods must be defined in the class.
WRONG class God { /* everything */ }
RIGHT Split into User, Auth, Profile, etc.
The Single Responsibility Principle: each class should have one reason to change. Large "God classes" are hard to test and maintain.

Frequently Asked Questions

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.