Skip to content

Instantly share code, notes, and snippets.

@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')
<?php
/**
* \NDN\Api\Bootstrap
* Bootstrap.php
*
* Bootstraps the API application
*
* @author Nikos Dimopoulos <nikos@niden.net>
* @since 2012-12-03
* @category Library
@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

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:

## 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 / 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
*/