Skip to content
Insights

Never* use arrays

keynote

Larry Garfield is a prominent speaker in the PHP community and has a lot of experience delivering talks. He’s one of the many top speakers that attended this conference. And, well, the message is quite clear: never* use arrays.
(*) in public

Why? Because it’s kind of a hack. Because values are not guaranteed to be of the same type. Because they have poor performance. And because PHP doesn’t have them... *gasp*

What Larry means is that the definition of an “array” is an ordered sequence of values of the same type, while in PHP arrays are associative arrays without type guarantees. Numeric arrays, where you don’t provide the key in the assignment, are a hack and behave weird (the keys are not always sequential).

Then he mentioned one of the greatest bugs of the last few years, namely Drupageddon (2014), which was also due to some code expecting numerical keys, but was (in certain conditions) causing an SQL injection risk.

So… What’s better? Purpose-built data structures! Do you know where he’s getting at? Yep, objects of course. And that’s where PHP has already everything you need. Did you know there’s an ArrayObject? Extend it to create a “TypedArray”. Or use the IteratorAggregate, Countable and Traversable interfaces to create sequences, sets, etc. They can be made to be unique, ordered, typed and everything you want.

But objects are slower, right? Nope…

Technique

Runtime (s)

Memory (bytes)

Associative array

9.4311 (n/a)

541,450,384 (n/a)

stdClass

11.2173 (+18.94%)

589,831,120 (+8.94%)

Public properties

8.2172 (-12.87%)

253,831,584 (-53.12%)

Private properties

11.0881 (+17.57%)

253,833,000 (-53.12%)

Anonymous class

8.1095 (-14.07%)

253,832,368 (-53.12%)

Check Larry’s slides for the benchmark he used, but the conclusion above is mind-blowing. 

Related

This article is part of the PHPBNL20 blogs

More insights

  • SymfonyCon 2025

    At Codana, we continue to lead the way in the world of custom software. That is why we were present once again last year at SymfonyCon 2025, held in Amsterdam's iconic convention center. With more than 30 talks spread across three tracks, one central theme emerged: how will we build even faster and more intelligent custom web apps in 2026?

    SymfonyCon 2025
  • Lightning Talks: Custom development x AI

    Recently, our experts shared their insights in two engaging lightning talks on how we integrate Artificial Intelligence (AI) to optimize our custom development processes, from the initial coding phase to refining the UX/UI design. This is a brief report on how we are bridging the gap between ideas and scalable, future-proof applications.

    Lightning Talks: Custom development x AI
  • Lightning Talks: The evolving role of AI in development

    The central question during our last Lightning Talks was: How can AI help us make this process smarter, faster, and more reliable, without replacing our human expertise?

    Lightning Talks: The evolving role of AI in development
  • Codana Lightning Talks | EAA Deadline & AI Code Editors

    The 2025 EAA deadline has passed. What now? Read our analysis of the accessibility law and a comparison of 4 AI assistants.

    Codana Lightning Talks | EAA Deadline & AI Code Editors
  • PHPverse 2025 Recap: Symfony, Laravel & AI

    Read our recap of PHPverse 2025. Discover key insights on FrankenPHP, Symfony, Laravel, AI, and the future of the language. 

    PHPverse 2025 Recap: Symfony, Laravel & AI
  • The European Accessibility Act (EAA): What Does It Mean for You?

    The European Accessibility Act (EAA): What Does It Mean for You?