DEV Community

Cover image for Object Calisthenics in PHP with Laravel: Improve Your Object-Oriented Code
Aleson França
Aleson França

Posted on

2

Object Calisthenics in PHP with Laravel: Improve Your Object-Oriented Code

Have you ever written Laravel code that works… but feels messy, hard to test, or hard to maintain?
That’s where Object Calisthenics comes in — a set of 9 simple rules that help you write cleaner, more focused, and maintainable object-oriented code.

What is Object Calisthenics?

Created by Jeff Bay, Object Calisthenics is a list of 9 rules to train good object-oriented habits.
Think of it as a workout for your code — maybe you won't follow all the rules every time, but practicing helps you improve.


The 9 Rules with PHP (Laravel) Examples.

Only One Level of Indentation per Method

Avoid deeply nested if statements. Extract logic into methods.

if (! $user->isActive()) return;
if (! $user->hasRole('admin')) return;

$this->grantAccess();
Enter fullscreen mode Exit fullscreen mode

Wrap All Primitives and Strings

Avoid passing raw values. Create Value Objects.

public function updateStatus(Status $status)
Enter fullscreen mode Exit fullscreen mode

Don’t Use the ELSE Keyword

Make your code easier to read by removing else.

if ($user->isActive()) return 'Active';

return 'Inactive';
Enter fullscreen mode Exit fullscreen mode

One Dot per Line

Avoid calling multiple methods in a chain — it creates tight coupling.

// Instead of
return $user->profile->address->city;

// Do
return $user->getCity();
Enter fullscreen mode Exit fullscreen mode

First Class Collections

Don’t use plain arrays — wrap them in a class.

class Order {
    public OrderItems $items;
}
Enter fullscreen mode Exit fullscreen mode

Don’t Abbreviate

Use clear and complete names.

// Bad
$usr = new UsrSvc();

// Good
$userService = new UserService();
Enter fullscreen mode Exit fullscreen mode

Keep All Entities Small

If a class is too long, it’s doing too much. Split it into smaller classes.


No Getters/Setters

Avoid exposing internals. Use intention-revealing methods.

// bad
$user->setName('John');

// good
$user->renameTo('John');
Enter fullscreen mode Exit fullscreen mode

No More Than Two Instance Variables per Class

Limit how much a class depends on.
If your class needs 4+ services, split the responsibilities.


Helpful Tools


Conclusion

Object Calisthenics isn’t a strict rule it’s a discipline.
By using these techniques in your PHP/Laravel projects, your code will become more:

  • Readable
  • Testable
  • Cohesive
  • Maintainable

Do you already follow any of these rules? Which one helped you the most?

Drop a comment below!

Top comments (2)

Collapse
 
xwero profile image
david duymelinck

As mentioned the rules are guides but I think they need more explanation.

There are some rules that are very sensible like thinking about the name of the methods and return as soon as possible. (rules 1, 3, 7, 9)

Then there is the rule to wrap arrays in a class. That is not always feasible, but if an array has a defined structure a class is a good way to validate that structure.

Keep classes small and limit dependencies. While the idea is good, any arbitrary limit is not going to work in reality.

And then there are the problematic rules:

  • Wrapping every variable in a object is just a waste of memory.
  • Don't use chaining. That was true before the nullsafe operator in PHP 8.0

The reason why those two rules are problematic, besides the mentioned reasons, is that they create too much abstraction for the task the code performs.

Collapse
 
michabbb_76 profile image
Michael Bladowski