PHP & Laravel Quick Revision: Data Types, OOP, MVC, Composer, and Best Practices

This guide will help you quickly revise PHP basics, OOP concepts, MVC, Composer, and modern best practices.

PHP Basics Refresher

πŸ”Ή Data Types in PHP

PHP supports 8 main data types:

  • String β†’ "Hello"
  • Integer β†’ 10
  • Float/Double β†’ 10.5
  • Boolean β†’ true / false
  • Array β†’ [1, 2, 3]
  • Object β†’ instance of a class
  • NULL β†’ no value
  • Resource β†’ special types (DB connection, file handles)

πŸ”Ή Loops in PHP

  • for β†’ fixed iterations
  • while β†’ runs while condition is true
  • do…while β†’ runs at least once, then checks condition
  • foreach β†’ loops arrays/objects

πŸ”Ή NULL vs Empty

  • NULL β†’ no value at all.
  • Empty β†’ value exists but is considered empty ("", 0, false, [], null).
    πŸ‘‰ All null are empty, but not all empty are null.

πŸ”Ή Arrays

  • Indexed array β†’ numeric keys
  • Associative array β†’ string keys
  • Multidimensional array β†’ arrays within arrays

πŸ”Ή Type Casting vs Type Juggling

  • Type Casting β†’ manual conversion: (int)"10"
  • Type Juggling β†’ PHP auto-converts when needed: "10" + 5 = 15

⚠️ Juggling can be risky. Protect against it by:

  • Using strict comparison ===
  • Enabling strict typing: declare(strict_types=1);

πŸ”Ή Error Control Operator

  • @ suppresses errors: $file = @file("missing.txt");
  • ⚠️ Bad practice β€” instead use try...catch.

Writing Professional PHP Code

Some best practices every PHP developer should follow:

  1. Enable strict types
  2. Use try…catch for error handling
  3. Use Composer for packages & autoloading
  4. Use require_once instead of require
  5. Follow PSR standards (PSR-1, PSR-4, PSR-12)
  6. Use namespaces
  7. Organize code with OOP & MVC
  8. Validate and sanitize input
  9. Use prepared statements/ORM for DB
  10. Store configs in .env files
  11. Use dependency injection instead of globals/singletons

Dependency Injection (DI) in PHP

  • Without DI: A class creates its own dependencies.
  • With DI: Dependencies are provided externally (constructor/method).

πŸ‘‰ Example:

class OrderService {
    private $db;
    public function __construct(Database $db) {
        $this->db = $db; // injected
    }
}

Benefits: loose coupling, testability, easier maintenance.
Laravel handles DI automatically via its Service Container.


PDO vs MySQLi

  • MySQLi β†’ works only with MySQL.
  • PDO β†’ works with multiple databases (MySQL, SQLite, PostgreSQL, etc.).
  • Laravel uses PDO by default.

Switch vs Match

  • switch β†’ loose comparison, needs break.
  • match (PHP 8+) β†’ strict comparison, returns value, no break.

Example:

echo match($x) {
    1 => "One",
    2 => "Two",
    default => "Other",
};

Variadic Functions

A variadic function accepts any number of arguments using ....

function sum(...$numbers) {
    return array_sum($numbers);
}

echo sum(1,2,3,4); // 10

Final Thoughts

PHP remains a solid backend choice, especially when paired with Laravel. By following PSR-12 standards, dependency injection, and Composer, you can write clean, professional, and maintainable code.

Modern PHP (with features like match, variadic functions, and strict types) is far more powerful than its old reputation suggests. Combine this with Laravel’s ecosystem, and you’re ready for both small apps and enterprise-level systems.


Let’s start with OOP principles.

There are 4 main principles of OOP (easy to remember with A PIE):

  1. A β†’ Abstraction
    • Show only essential details, hide complexity.
    • Example: You use car->start() without knowing how engine works.
  2. P β†’ Polymorphism
    • One function/method can behave differently depending on context.
    • Example: draw() works differently for Circle and Square.
  3. I β†’ Inheritance
    • Child class gets properties & methods from parent class.
    • Example: Dog inherits from Animal.
  4. E β†’ Encapsulation
    • Bundle data & methods together, restrict access with public/private/protected.
    • Example: Bank balance can’t be changed directly, only via deposit() method.

πŸ‘‰ Memory Trick:
Think of OOP as eating β€œA PIE” 🍰 β†’ Abstraction, Polymorphism, Inheritance, Encapsulation.

βœ… That’s your revision summary blog post for today!