DEV Community

Cover image for PHP: Stop Passing Arrays Everywhere

PHP: Stop Passing Arrays Everywhere

Lars Moelleken on January 25, 2026

Designing Explicit Data Contracts #blogPostAsWebApp: https://voku.github.io/PHPArrayBox/ Rule of thumb: If your array-shape PHPDoc ne...
Collapse
 
thormeier profile image
Pascal Thormeier • Edited

Funnily enough, around 5 minutes ago, I was juggling with render arrays in a Drupal 11 instance... I see why they still have them, though, legacy and all, but gosh, I so wish they had some actual OOP. The worst part is that they're mixing these deeply nested arrays that only work by convention with value objects.

Great post, thank you! I kinda needed to read this to know that I'm not the only person out there to think that this isn't sustainable.

Collapse
 
xwero profile image
david duymelinck

Let PHPStan enforce contracts

I let PHP enforce contracts. The problem I have with PHPStan to enforce contracts is that bugs can still occur when running the code in production.
I don't want to let PHPStan be the Typescript of PHP.

Collapse
 
suckup_de profile image
Lars Moelleken
  • Runtime errors means errors for users
  • High phpstan level shape your architecture for good (+ in the end also good autocompletion in the IDE)
  • nobody stops you to force things with php and still use phpstan (+ often phpstan force you to use types and interfaces etc. from php)
  • in the end nothing but real thinking prevents logic bugs 😊
Collapse
 
xwero profile image
david duymelinck

To be clear, I'm not against PHPStan.
It is the array<string, scalar> and non-empty-string comments that gives me the creeps.
In the end PHPStan is an additional tool. It shouldn't be considered as part of the language.

Thread Thread
 
suckup_de profile image
Lars Moelleken

Why not, even the language developers from php itself though of this more than once, same as hack (Facebook) who has introduced Generics since many years this way. docs.hhvm.com/hack/reified-generic...

Thread Thread
 
xwero profile image
david duymelinck

I think you misunderstood the last sentence of my comment.
It is not that I don't want generics in PHP, I don't want generics to handled only by PHPStan.

Thread Thread
 
suckup_de profile image
Lars Moelleken

I understand, that's why I used that example..., what if the php foundation would include it into php, same as hack did it? (Hack supports generics for type safety, but by default they are erased at runtime (type erasure).)

Collapse
 
tkouleris profile image
Thodoris Kouleris

At my first job as a junior developer, my boss built a php framework that was passing arrays in every little function that he created. Although he was a seasoned developer I believe he liked the simplicity of the array.

Me and my colleagues tried to change his mind. He somehow backed up by accepting laravel as a framework but still....You could see that he loved his precious arrays :)

Collapse
 
suckup_de profile image
Lars Moelleken

You can generate a phpstan baseline file, where you ignore all old errros, and just start to write maintainable code.

For frameworks and other tools there are many phpstan extensions that supports you e.g. auto detect types directory from your sql queries or other magic stuff, if you really need it.

 
suckup_de profile image
Lars Moelleken

yes, it always depends on the project and the costs that the company has if something breaks in production.

Collapse
 
jochen_califice_eb1108bd6 profile image
Jochen Califice

Nice post! Should be required reading for any PHP developer! Good work!