Skip to content

Instantly share code, notes, and snippets.

View J7mbo's full-sized avatar
🔨
Working with distributed systems

James Mallison J7mbo

🔨
Working with distributed systems
View GitHub Profile
[**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained, are [officially deprecated](http://j.mp/XqV7Lp) and can be [dangerous in live code](http://bit.ly/4zUdtT). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ).

Please, don't use mysql_* functions in new code. They are no longer maintained, are officially deprecated and can be dangerous in live code. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, [here is a go

@J7mbo
J7mbo / PHP Reflection Haxx
Created April 9, 2013 14:49
Haxx your way into a Class's private / protected variables using Reflection and variable variables! Pretty neat!
<?php
class Parameters
{
protected $var1;
protected $var2;
protected $var3;
public function __construct($one, $two, $three)
{
$this->var1 = $one;
<?php
class B {
protected $c;
public function __construct(C $c) {
$this->c = $c;
}
}
class C {}
@J7mbo
J7mbo / MySQL PDO Select Abstraction
Last active December 17, 2015 01:59
Abstraction for MySQL PDO SELECT statement. Running `findByValue(array('id', 'username'), 'users', array('website' => 'j7mbo.co.uk'), true);` is the same as: `SELECT id, username FROM `users` WHERE website = 'j7mbo.co.uk' LIMIT 1;`
public function findByValue($values, $location, $requirements, $single = false)
{
$return = array();
$conn = $this->conn;
$query = 'SELECT ' . implode(", ", $values) . ' FROM ' . $location . ' WHERE';
$i = 0;
foreach ($requirements as $key => $value)
{
@J7mbo
J7mbo / gist:6388265
Created August 30, 2013 09:57
ALMOST awesomeness
<?php
// EchoEndpoint.php
use Aerys\Handlers\Websocket\Message,
Aerys\Handlers\Websocket\Endpoint,
Aerys\Handlers\Websocket\WebsocketHandler,
Alert\Reactor,
Amp\JobDispatcher,
Amp\CallResult;
@J7mbo
J7mbo / gist:6388474
Created August 30, 2013 10:24
aerys conf
<?php
use Aerys\Config\Bootstrapper;
use Aerys\Handlers\ReverseProxy\ReverseProxyLauncher;
use Aerys\Handlers\DocRoot\DocRootLauncher;
require dirname(__DIR__).'/vendor/autoload.php';
require '/var/www/Aerys/autoload.php';
require __DIR__ . '/EchoEndpoint.php';
<VirtualHost *:1337>
ServerName silex.local
ServerAlias silex.local
DocumentRoot "/home/james/Dev/github/Silex/web"
DirectoryIndex index.php
<Directory "/home/james/Dev/github/Silex/web">
AllowOverride All
@J7mbo
J7mbo / gist:6796657
Last active December 24, 2015 12:19
An Instance controller for aws. Not hard.
<?php
namespace Classes;
class InstanceController
{
/**
* Ubuntu 10.04 LTS AMI
*
* @link <http://cloud-images.ubuntu.com/locator/ec2/>
/**
* User subscribes with their subject:userId:token
*
* If token does not match userId (check DB), return a 403 else...
*
* Token is regenerated and placed in DB for next time the page is visited,
* userId is retrieved that matches token.
*
* New process spawned which runs looping zmq with argv being userId from
* previous DB check. Process Id retrieved.
@J7mbo
J7mbo / gist:6825287
Created October 4, 2013 12:39
An example of a torrent handler, some factories, torrent class and dispatcher.
<?php
interface TorrentHandler
{
public function retrieveTorrents();
public function getTorrent($id);
public function startTorrent(TorrentInterface $torrent);
public function stopTorrent(TorrentInterface $torrent);
public function pauseTorrent(TorrentInterface $torrent);
public function deleteTorrent(TorrentInterface $torrent);