Skip to content

Instantly share code, notes, and snippets.

View mpratt's full-sized avatar

Michael Pratt mpratt

View GitHub Profile
@mpratt
mpratt / Session.php
Last active April 14, 2016 12:36
A wrapper for session management
<?php
/**
* Session.php
*
* @package Bolido
* @author Michael Pratt <pratt@hablarmierda.net>
* @link http://www.michael-pratt.com/
* @license MIT
*/
@mpratt
mpratt / Random.php
Created December 12, 2013 21:42
A random number/range/string/bool generator
<?php
/**
* Random.php
*
* @author Michael Pratt <pratt@hablarmierda.net>
* @link http://www.michael-pratt.com/
* @license MIT
*
*/
@mpratt
mpratt / Mailer.php
Last active December 31, 2015 04:39
A simple Mailer class
<?php
/**
* Mailer.php
*
* @author Michael Pratt <pratt@hablarmierda.net>
* @link http://www.michael-pratt.com/
* @license MIT
*/
namespace Mailer;
@mpratt
mpratt / Ejemplo1.php
Last active January 12, 2020 12:35
Patrones de Diseño: Observador (Observer)
<?php
/**
* Clase Partido
* En este caso este es nuestro sujeto
*/
class Partido implements \SplSubject
{
protected $teams = array();
protected $observers = array();
<?php
$nombre = $_POST['nombre'];
$password = $_POST['pass'];
// Validamos $nombre, bla bla bla..
// Extraemos el hash de la base de datos
$db = new PDO(......);
$stmt = $db->prepare('SELECT pass
FROM usuarios
<?php
class Damage
{
protected $name;
protected $damagePoints = 10;
public function __construct($name)
{
$this->name = $name;
}
<?php
class Logger
{
protected $file;
public function __construct($file) { $this->file = $file; }
public function log($message)
{
$message = date('H:i:s ') . $message . PHP_EOL;
@mpratt
mpratt / anonima.php
Last active December 16, 2015 03:59
De PHP 5.1 a 5.5
<?php
// Tenemos un arreglo con números
$misNumeros = array('1', '2', '3', '4', '5', '6');
/* Pero resulta que solo queremos los numeros impares,
así que usamos array_filter con una función anonima,
en vez de declarar la funcion antes. */
print_r(array_filter($misNumeros, function($n) { return ($n%2 != 0); }));
/* Resulta en: Array([0] => 1, [2] => 3, [4] => 5) */
<?php
/**
* ImageManager.php
* A wrapper around GD that does multiple operations like, resizing, rotating,
* cropping and stuff.
*
* @author Michael Pratt <pratt@hablarmierda.net>
* @version 1.0
* @link http://www.michael-pratt.com/
* @demo http://www.michael-pratt.com/Lab/imageManager/
SELECT id, nombre, (6371 * ACOS(
SIN(RADIANS(lat)) * SIN(RADIANS(4.6665578))
+ COS(RADIANS(lng - -74.0524521)) * COS(RADIANS(lat))
* COS(RADIANS(4.6665578))
)
) AS distance
FROM direcciones
HAVING distance < 1 /* 1 KM a la redonda */
ORDER BY distance ASC