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

Cross-platform applicaties with React Native

Never before has developing native mobile applications been as accessible as it is today. At Codana, we do this by using the React Native, an open-source framework developed by Meta.

Author: Jinse Camps
Architect | Analyst
Jinse Camps
dev

Laracon EU 2024

A fantastic learning experience to inspire and be inspired together with a lot of other Laravel passionate people! Something we couldn't miss and very much connect with the community. What a top event! Who will we see next editions? 😮

Author: Noah Gillard
PHP / Laravel Developer
Noah Gillard AI generated Face
laracon codana persoon

An efficient tourism data management system

A TDMS or Tourist Data Management System, is simply a platform that retrieves data from various sources, processes it internally either automatically or not, and offers this data back to external platforms.

Author: Tom Van den Eynden
Web Architect | Coordinator
Tom Van den Eynden
laptop

Tourism Data Management Systems

In dit artikel verkennen we wat een TDMS is, waarom het essentieel is voor de toerisme-industrie, en hoe technologieën zoals Laravel en ElasticSearch het verschil kunnen maken. 

Author: Tom Van den Eynden
Web Architect | Coordinator
Tom Van den Eynden
tdms

The difference between data management and data processing in a digital economy

Gegevens zijn cruciaal voor bedrijven en het begrijpen van de verschillen tussen gegevensbeheer en gegevensverwerking kan verwarrend zijn. In dit artikel zullen we deze verschillen in de digitale economie nader bekijken om hun doelen en toepassingen beter te begrijpen.

Author: Tom Van den Eynden
Web Architect | Coordinator
Tom Van den Eynden
gegevensverwerking

Test Driven Development - application to a project

TDD, or in full Test Driven Development, is an approach to development where we start from writing tests.

Author: Sarah Jehin
PHP developer
Sarah Jehin
development