Skip to content

Instantly share code, notes, and snippets.

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:

@niden
niden / Model.php
Last active December 21, 2015 05:29
Model with Query Buidler
<?php
/**
* \ND\Model
* Model.php
*
* Model
*
* @author Nikos Dimopoulos <nikos@niden.net>
* @since 2012-12-12
* @category Library
<?php
/**
* \NDN\Api\Bootstrap
* Bootstrap.php
*
* Bootstraps the API application
*
* @author Nikos Dimopoulos <nikos@niden.net>
* @since 2012-12-03
* @category Library
@niden
niden / CIDR.php
Last active August 29, 2015 14:07 — forked from jonavon/CIDR.php
<?php
/**
* CIDR.php
*
* Utility Functions for IPv4 ip addresses.
* Supports PHP 5.3+ (32 & 64 bit)
* @author Jonavon Wilcox <jowilcox@vt.edu>
* @revision Carlos Guimarães <cvsguimaraes@gmail.com>
* @version Wed Mar 12 13:00:00 EDT 2014
*/
## Configure eth0
#
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
NM_CONTROLLED="yes"
ONBOOT=yes
HWADDR=A4:BA:DB:37:F1:04
TYPE=Ethernet
BOOTPROTO=static
@niden
niden / send.php
Last active January 21, 2016 21:31
Send file
// Send it out to the client
$response = new PhResponse();
$response
->setContentType('application/pdf')
->setHeader('Content-Description', 'File Transfer')
->setHeader('Content-Disposition', sprintf('attachment; filename=%s.pdf', $filename))
->setHeader('Content-Transfer-Encoding', 'binary')
->setHeader('Connection', 'Keep-Alive')
->setHeader('Expires', 0)
->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
@niden
niden / commit-msg
Last active January 29, 2017 17:20
Git hooks
#!/bin/bash
# This way you can customize which branches should be skipped when
# prepending commit message.
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop test)
fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.(\d+)\.(bmp|css|cur|gif|ico|jpe?g|js|png|svgz?|webp|webmanifest)$ $1.$3 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
@niden
niden / php_evaluation_order.md
Created April 15, 2019 15:10 — forked from nikic/php_evaluation_order.md
Analysis of some weird evaluation order in PHP

Order of evaluation in PHP

Yesterday I found some people on my [favorite reddit][lolphp] wonder about the output of the following code:

<?php

$a = 1;
$c = $a + $a++;