PHP's Oddities
Recorded: May 23, 2026, 3:58 p.m.
| Original | Summarized |
PHP's Oddities | flow2 Home Home In Tech PHP's Oddities May 19, 2026 I've been coding in PHP at work for the last 5 years. My org's entire backend is written in PHP—a decision made in 2007 when the company first started. It's not a language I ever imagined myself using prior to working there, but life takes you in all sorts of directions you don't expect. Arrays are weird and overloaded Arrays Are Not Really Arrays // you can count em' // you can access em' // you can print em' Everything looks fine but you get into trouble whenever you perform a mutation on this "simple" array; it will be exposed as being a key-value store. // getting the first element won't work anymore why can't I hold all these indices??? It's just strange to me that PHP doesn't support simple collections of objects. It's annoying to have to manage these arbitrary numeric keys when all you really want is ordinal indexing like 99% of the time. It feels like a leaky abstraction. Here, I'm illustrating all the ways of declaring the type for a string property: Don't Before PHP7, all class properties were (1): untyped. Since the type system is optional, it has to live alongside the "legacy" behaviour which has weird consequences. For example, what do you think the values of these three properties will be after we instantiate a Book object? Trick question! Only the untyped $title property will have a value, and that value is NULL. That seems fine and is roughly in line with how I'd expect a language to use a NULL value. But the other two properties will NOT have a value because they don't exist, or rather they could exist but haven't been initialized yet. Not a warning—a FATAL error occurs if you try to access an uninitialized property. This comes up a lot in cases where you try to deserialize data into a PHP object. If a field's data isn't present you might not initialize the property at all. ahh yes, NULL...who was that by again? So I feel like the class property type system does little to help you understand what a given object is composed of, and in some respects has made it less clear because it's introduced this new uninitialized state. As a developer, it's hard to write defensive code because you're never sure which checks to do for all these situations: is_null(), isset(), property_exists(), empty()... it's not obvious which functions cover which states. It's a scripting language, so development friction is low. Make a file change and it instantly takes effect. Thanks for reading! #coding #languages #PHP Previous Post Next Post Copyright © 2026 Joe Boudreau |
The author reflects on their five years of working with PHP, noting that despite PHP being a mature and widely used language, it carries a reputation that stems partly from outdated understandings and certain unintuitive language features that have historically caused bugs. The author highlights two primary areas where PHP exhibits these oddities: its handling of arrays and its type system for class properties. Regarding arrays, the author posits that PHP’s standard library is heavily centered around the array data structure, which is technically an ordered key-value dictionary rather than a traditional array. This design choice introduces complexity, particularly when performing mutations or operations on these structures. When built-in functions such as sorting or filtering are applied to an array, they operate on both the keys and values, which can lead to inconsistent key ordering when the array is mutated in-place. This behavior often results in unexpected outcomes, such as the loss of expected array indices or the inability to access elements after operations like array_filter or unset. To mitigate these issues and ensure predictable behavior, the author points out the necessity of using functions like array_values() to reset the indices after mutable operations, emphasizing that developers must be acutely aware of the array's internal structure to avoid subtle bugs. The second area of complexity concerns PHP's type system, especially in relation to class properties. Although type declarations were added incrementally, the system inherits quirks from its dynamic nature, leading to an "uninitialized state" that complicates defensive programming. The author illustrates this with an example involving a class where properties are declared with specific types, such as strings or nullable strings. Before PHP7, or in the general dynamic context, class properties can exist in an uninitialized state that is distinct from being null. When an object is instantiated, properties that are not explicitly assigned might not exist at all, leading to fatal errors when attempting to access them. This distinction between an uninitialized state and a null value creates confusion for developers attempting to implement robust null checks; checking for null does not cover the possibility of an uninitialized property, which is exacerbated when properties can be dynamically added to objects. The author suggests that this lax behavior makes writing defensive code difficult, as developers must constantly consider properties that are unset versus those that simply hold a null value, often requiring the use of functions like isset(), is_null(), and property_exists() to navigate these subtle states. In conclusion, despite these noted architectural oddities, the author maintains that the negative perception of PHP is somewhat undeserved. The language possesses understandable tradeoffs, but it remains capable of accomplishing any task. The author appreciates aspects of PHP, such as the low development friction inherent in its scripting nature and the robust functionality provided by frameworks like Laravel. Ultimately, the author concludes that gaining a deeper understanding of a language allows a developer to structure code to work with its inherent characteristics, leading to more idiomatic and effective programming. |