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:

@meniam
meniam / php_remove_empty_values_from_array.php
Last active December 20, 2015 02:28
Remove empty values from php array
<?php
// Remove NULL values only
$new_array_without_nulls = array_filter($array_with_nulls, 'strlen');
// Remove any FALSE values
$new_array_without_nulls = array_filter($array_with_nulls);
?>
@meniam
meniam / _autoload.php
Created July 22, 2013 18:44
Zend Autoloader
// if composer autoloader is missing, explicitly add the ZF library path
require_once __DIR__ . '/../vendor/' . 'zendframework/library/Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(
array(
Zend\Loader\StandardAutoloader::LOAD_NS => array(
'Zend' => __DIR__ . '/../vendor/zendframework/library/Zend',
'Model' => __DIR__ . '/../Model',
'ModelTest' => __DIR__ . '/ModelTest',
),
));
@meniam
meniam / explode plus list
Created July 24, 2013 14:38
When you use list($a, $b) = explode('delimiter', $str) and in str delimiter doesn't exists php show warning
if (count($segments = explode('By', $method, 2)) == 2) {
list($basePart, $by) = $segments;
} else {
$basePart = $method;
$by = '';
}
@meniam
meniam / BEM-XJST.md
Created November 16, 2013 18:45 — forked from tadatuta/BEM-XJST.md

BEM-XJST

Это БЭМ-ориентированные хелперы над стандартным XJST-синтаксисом. Сюда входит:

  • синтаксический сахар для подпредикатов про БЭМ предметную область
  • возможность писать вложенные шаблоны
  • синтаксический сахар для apply по какой-то mode (apply(this._mode = 'bla'))
  • ключевое слово applyCtx (синтаксический сахар для applyNext(this.ctx = { some: 'new' }))
Подпредикаты про БЭМ
@meniam
meniam / gist:7886682
Created December 10, 2013 06:49 — forked from fomigo/gist:2382775
<?php
/*
echo plural_form(42, array('арбуз', 'арбуза', 'арбузов'));
*/
function plural_form($n, $forms) {
return $n%10==1&&$n%100!=11?$forms[0]:($n%10>=2&&$n%10<=4&&($n%100<10||$n%100>=20)?$forms[1]:$forms[2]);
}
location /resize {
alias /tmp/nginx/resize;
set $width 150;
set $height 100;
set $dimens "";
if ($uri ~* "^/resize_(\d+)x(\d+)/(.*)" ) {
set $width $1;
set $height $2;
set $image_path $3;
sudo apt-get update
sudo apt-get install python-virtualenv
sudo apt-get install python-dev
sudo apt-get install postgresql
sudo apt-get install postgresql-server-dev-9.3
sudo apt-get install redis-server
sudo -u postgres createuser -s sentry
sudo -u postgres psql -c "alter user sentry with password 'sentry';"
# Credit http://stackoverflow.com/a/2514279
for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --format="%ci %cr" $branch | head -n 1` \\t$branch; done | sort -r
@meniam
meniam / getAbsoluteUrl.js
Last active January 28, 2016 00:15
getAbsoluteUrl - Получить абсолютный URL
var getAbsoluteUrl = (function() {
var a;
return function(url) {
if(!a) a = document.createElement('a');
a.href = url;
return a.href;
};
})();