Featured image of post Understanding PHP Classes and Good Practices for OO and Isolation in PHP

Understanding PHP Classes and Good Practices for OO and Isolation in PHP

Understanding PHP Classes and Good Practices for OO and Isolation in PHP

w

Understanding PHP Classes and Good Practices for OO and Isolation in PHP

Introduction

PHP is like that one friend who insists on being everywhere.
It powers websites, APIs, and even that janky CMS your boss won’t let go of.
But if you don’t organize your PHP code properly, it turns into a tangled mess of spaghetti.

This is where Object-Oriented Programming (OOP) and good coding practices come in.
In this article, we’ll take a deep dive into PHP classes, best practices for isolation, and how to structure PHP code—even when you’re flying solo without a framework.

What Are PHP Classes?

A PHP class is basically a blueprint for creating objects.
Think of it like a cookie cutter: the class defines the shape, and the objects are the delicious cookies.

Basic PHP Class Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?php

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

    public function bark() {
        return "{$this->name} says Woof!";
    }
}

$dog = new Dog("Buddy");
echo $dog->bark(); // Output: Buddy says Woof!

?>

Best Practices for OOP in PHP

1. Use Encapsulation (Keep Your Stuff Private)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php

class BankAccount {
    private $balance;

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

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

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

$account = new BankAccount(100);
$account->deposit(50);
echo $account->getBalance(); // Output: 150

?>

2. Follow the SOLID Principles

  • Single Responsibility Principle (SRP) – A class should do one thing well.
  • Open/Closed Principle (OCP) – Classes should be open for extension, but closed for modification.
  • Liskov Substitution Principle (LSP) – Subclasses should be replaceable with their parent classes.
  • Interface Segregation Principle (ISP) – Don’t force classes to implement methods they don’t need.
  • Dependency Inversion Principle (DIP) – Depend on abstractions, not concrete implementations.

3. Use Dependency Injection (DI)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php

class Logger {
    public function log($message) {
        echo "[LOG]: $message";
    }
}

class UserService {
    private $logger;

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

    public function createUser($name) {
        $this->logger->log("User '$name' created.");
    }
}

$logger = new Logger();
$userService = new UserService($logger);
$userService->createUser("Alice");

?>

What is IoC (Inversion of Control)?

IoC lets external sources manage dependencies, reducing tight coupling.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php

class Container {
    private $bindings = [];

    public function bind($key, $resolver) {
        $this->bindings[$key] = $resolver;
    }

    public function make($key) {
        return call_user_func($this->bindings[$key]);
    }
}

$container = new Container();
$container->bind("logger", function() {
    return new Logger();
});

$logger = $container->make("logger");
$logger->log("Hello from IoC!");

?>

How to Organize PHP Code Without a Framework

1. Use a Proper Directory Structure

1
2
3
4
5
6
7
8
9
/myapp
  /src
    /Controllers
    /Models
    /Services
    /Repositories
  /config
  /public
  /vendor

2. Use an Autoloader

1
2
3
4
5
6
7
{
  "autoload": {
    "psr-4": {
      "App\\": "src/"
    }
  }
}

Run composer dump-autoload to apply it.

3. Separate Business Logic from Controllers

Controllers should only handle requests and responses—the heavy lifting should be done in services or repositories.

4. Use Environment Variables for Configurations

1
2
3
DB_HOST=localhost
DB_USER=root
DB_PASS=supersecurepassword

Use getenv('DB_HOST') in PHP to access them.

Conclusion

Writing maintainable PHP is like keeping a toddler entertained—if you don’t do it right, things get messy fast.

Follow OOP principles, use DI and IoC, and organize your code properly, and you’ll have a clean, testable, and scalable PHP application.


Key Ideas

ConceptExplanation
ClassesBlueprints for objects
EncapsulationKeeping properties private
SOLIDFive principles for better OOP
Dependency InjectionInject dependencies instead of hardcoding
IoCLetting an external container manage dependencies
AutoloadingUse Composer’s PSR-4 autoloader
Directory StructureKeep controllers, models, and services separate

References