Skip to content

Instantly share code, notes, and snippets.

@nikic
nikic / objects_arrays.md
Last active May 16, 2025 22:07
Post explaining why objects often use less memory than arrays (in PHP)

Why objects (usually) use less memory than arrays in PHP

This is just a small post in response to [this tweet][tweet] by Julien Pauli (who by the way is the release manager for PHP 5.5). In the tweet he claims that objects use more memory than arrays in PHP. Even though it can be like that, it's not true in most cases. (Note: This only applies to PHP 5.4 or newer.)

The reason why it's easy to assume that objects are larger than arrays is because objects can be seen as an array of properties and a bit of additional information (like the class it belongs to). And as array + additional info > array it obviously follows that objects are larger. The thing is that in most cases PHP can optimize the array part of it away. So how does that work?

The key here is that objects usually have a predefined set of keys, whereas arrays don't:

@hugocf
hugocf / bash.sh
Last active February 21, 2021 23:01
Moved to ➔ https://github.com/hugocf/scripts/blob/master/templates/bash.sh — Template for interactive bash scripts: supports command line options with values; prompts for any missing parameters/arguments.
#!/usr/bin/env bash
# Created by Hugo Ferreira <[email protected]> on isoD
# Licensed under the MIT License: https://opensource.org/licenses/MIT
set -u # treat unset variables as errors
readonly BASEDIR=$(cd "$(dirname "$0")" && pwd) # where the script is located
readonly CALLDIR=$(pwd) # where it was called from
readonly SUCCESS=0 # exit status of bash commands