🙋
Everyone says Laravel is “object-oriented”. What does that mean, in normal words?
💡
OOP (Object-Oriented Programming) means organising code like things in real life. A car has data (colour, speed) and actions (drive, honk). In code, that bundle is called an object.
🙋
And the “4 pillars”?
💡
They're 4 good habits for building those objects. Learn them once and you'll recognise them in every Laravel file. Each one has a demo you can click. 👇
Warm-up

Class vs object in 10 seconds

🏠 Real life
🍪

A class is a cookie cutter. Objects are the cookies. One cutter can make 100 cookies, each with its own sprinkles.

=
💻 In code

          class Cookie                  // the cutter
          {
              public function __construct(
                  public string $sprinkles
              ) {}
          }

          $a = new Cookie('rainbow');   // a cookie
          $b = new Cookie('chocolate'); // another cookie
        
The big four

Tap a pillar to jump to it

🔒

1 · Encapsulation

Protect your data. Only let people change it through safe buttons.

🏠 Real life
🏧

An ATM. You can deposit and withdraw using the buttons, but you can't open the machine and grab the cash. The machine also checks the rules: “not enough money? No cash for you.”

=
💻 In code

          class BankAccount
          {
              private int $balance = 100;   // 🔒 locked inside

              public function deposit(int $amount): void
              {
                  if ($amount <= 0) throw new Exception('Amount must be positive.');
                  $this->balance += $amount;
              }

              public function withdraw(int $amount): void
              {
                  if ($amount > $this->balance) throw new Exception('Not enough money!');
                  $this->balance -= $amount;
              }

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

The keywords are the locks: public means anyone can use it, protected means only this class and its children, and private means only this class. Data is usually private; the buttons (methods) are public.

Remember it like this

Encapsulation = a capsule. The medicine (data) is sealed inside; you only interact with the outside.

ℹ️ Where you'll see it in Laravel

A model's $fillable list decides which fields can be filled from outside, and $hidden keeps the password out of JSON. Same idea: protect the inside.

🧬

2 · Inheritance

Don't rewrite what a parent already has. Inherit it, then add your own extras.

🏠 Real life
🚙➜🏎️

Every car can drive and honk. A sports car is a car, so it gets those for free, and it adds a turbo button. Nobody re-invents the wheel. Literally. 😄

=
💻 In code

          class Car
          {
              public function drive() { return 'Vroom! Driving…'; }
              public function honk()  { return 'Beep beep!'; }
          }

          class SportsCar extends Car   // 🧬 inherits drive() + honk()
          {
              public function turbo() { return '🔥 TURBO BOOST!'; }
          }
        

The magic word is extends. The child (SportsCar) gets every public and protected method of the parent (Car). It can also override one by writing a method with the same name.

Remember it like this

Ask: “Is a SportsCar a Car?” If the answer is “yes, it is a…”, inheritance fits.

ℹ️ Where you'll see it in Laravel

class Post extends Model. That one word gives your tiny Post class hundreds of inherited powers: save(), delete(), find(), where()… you write none of them.

📺

3 · Abstraction

Show only what people need. Hide the complicated stuff.

🏠 Real life
🎮

A TV remote has a few simple buttons. You press “Volume +” without knowing about infrared signals, chips or amplifiers. The complexity exists, it's just hidden.

=
💻 In code

          interface Remote               // 📋 the buttons you get
          {
              public function power(): void;
              public function volumeUp(): void;
              public function nextChannel(): void;
          }

          // Some class does the hard work behind these buttons.
          // You only ever talk to the buttons.
        

In PHP, abstraction usually means an interface (a list of buttons, with no wiring) or an abstract class (some buttons wired, some left for the child to finish). Both say what can be done, not how.

Remember it like this

Abstraction = the menu, not the kitchen. You order “pasta” without watching the chef cook.

ℹ️ Where you'll see it in Laravel

Mail::to($user)->send(new Welcome) sends an email. You never touch SMTP servers or encoding. Cache::put() works the same whether the cache is a file, Redis or a database.

🎭

4 · Polymorphism

“Poly” = many, “morph” = shapes. One command, many behaviours.

🏠 Real life
🐶🐱🐮

Say “speak!” to a dog, a cat and a cow. Same command, but you get “woof”, “meow” and “moo”. You don't need a different command for each animal.

=
💻 In code

          interface PaymentMethod
          {
              public function pay(int $amount): string;
          }

          class CardPayment implements PaymentMethod {
              public function pay(int $amount): string { return "Charged card {$amount}"; }
          }

          class PayPalPayment implements PaymentMethod {
              public function pay(int $amount): string { return "Paid {$amount} via PayPal"; }
          }

          function checkout(PaymentMethod $method, int $amount) {
              return $method->pay($amount);   // 🎭 works for ANY method
          }
        

The checkout() function doesn't care which payment it gets, only that it can pay(). Adding Apple Pay tomorrow? Write one new class. Checkout stays untouched.

Remember it like this

Polymorphism = a universal remote. The same “play” button works on the TV, the speaker and the projector. Each device plays in its own way.

ℹ️ Where you'll see it in Laravel

Change MAIL_MAILER=smtp to MAIL_MAILER=log in .env and your code keeps calling the same send(). A different “driver” class just handles it differently. That's polymorphism powering Laravel's drivers.

Cheat sheet

All four on one card

PillarIn one lineReal lifePHP keyword
🔒 EncapsulationProtect data; change it only through methodsATMprivate / public
🧬 InheritanceReuse a parent's code and add extrasCar → Sports carextends
📺 AbstractionShow simple buttons, hide the wiringTV remoteinterface / abstract
🎭 PolymorphismSame method name, different behaviour“Speak!” to petsimplements + same method

Want the deeper version of each? Visit Encapsulation, Inheritance, Abstraction and Polymorphism in Module 02.

Common mistake

Avoid this, do that

❌ Avoid

          if ($type === 'card') { /* … */ }
          elseif ($type === 'paypal') { /* … */ }
          elseif ($type === 'crypto') { /* … */ }
        

Every new payment method means editing this growing if-chain, and it's easy to break.

✅ Do

          $method->pay($amount);
        

Let each class handle itself (polymorphism). New method? New class. Nothing else changes.

🏋️ Try it yourself
  1. In the bank demo, try the hack button. What protects the balance?
  2. Write a Dog and a Cat class that both implement an Animal interface with speak().
  3. Create ElectricCar extends Car and override honk() to return “Beep (quietly)”.
  4. Open any model in a Laravel project. Can you spot inheritance (extends) and encapsulation ($fillable, $hidden)?
Recap

Key takeaways

Check yourself

Mini quiz