Skip to content

Instantly share code, notes, and snippets.

View dhotson's full-sized avatar

Dennis Hotson dhotson

View GitHub Profile
require 'eventmachine'
EM.run do
EM.epoll
# Create a channel to push data to
EventChannel = EM::Channel.new
# The server simply subscribes client connections to the channel on connect,
# and unsubscribes them on disconnect.
@dhotson
dhotson / gist:295737
Created February 5, 2010 11:59
A little Object Relational Mapper in PHP
<?php
/**
* A little Object Relational Mapper in PHP
* @author dennis.hotson@gmail.com
*/
class Post
{
public $title;
#!/usr/bin/env php
<?php
chdir(dirname(__FILE__));
require_once '../lib/kelpie/kelpie_init.php';
require_once('../includes/myframework.php');
class MyApp
{
public function __construct()
<?php
class DomainObject
{
public function __construct($arr = array())
{
foreach($arr as $key => $value)
$this->$key = $value;
$this->_attributes = new Attributes();
<?php
$english = arr(
strr('Hello X'),
strr('Goodbye X')
);
$pirate = $english
->replace('/Hello/', 'Avast')
->replace('/Goodbye/', 'Was good te see ye')
jQuery.fn.noisy = function(opacity) {
if (typeof(opacity) === 'undefined') {
opacity = 0.1;
}
var wrapper = jQuery(this).wrapInner('<div />').children();
var canvas = document.createElement("canvas");
canvas.width = 100;
canvas.height = 100;
var ctx = canvas.getContext("2d");
<?php
// Define the 'class' class
$class = Obj()
->fn('new', function ($class) {
$newClass = Obj($class->methods)
->fn('new', function($class) {
$obj = Obj($class->imethods);
$args = func_get_args();
array_shift($args);
<?php
require_once 'oo.php'
// A subclass of 'class' that lets you define classes in XML ;-)
$xmlclass = $class->extend()
->fn('load', function($t, $xml) {
$class = $t->new();
$doc = new DOMDocument();
$doc->loadXML($xml);
<?php
$array = function() {
$elements = func_get_args();
$result = fn(function($i) use ($elements) {
return $elements[$i];
});
$result->len = function() use ($elements) {
@dhotson
dhotson / oo.php
Created December 6, 2011 12:02
PHP Object Oriented Programming Reinvented (for PHP 5.4)
<?php
// Define the 'class' class
$class = (new Obj)
->fn('new', function() {
$newClass = (new Obj($this->methods))
->fn('new', function() {
$obj = new Obj($this->imethods);
call_user_func_array(array($obj, 'init'), func_get_args());
return $obj;