Skip to content

Instantly share code, notes, and snippets.

View gooh's full-sized avatar

Gordon Oheim gooh

View GitHub Profile
@gooh
gooh / php_errors.txt
Created July 27, 2011 16:23 — forked from ircmaxell/php_errors.txt
All errors in the 5.3 source code tree (zend_error|php_error_docref)
./ext/bcmath/bcmath.c: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Division by zero");
./ext/bcmath/bcmath.c: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Division by zero");
./ext/bcmath/bcmath.c: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Square root of negative number");
./ext/bz2/bz2.c: php_error_docref(NULL TSRMLS_CC, E_WARNING, "length may not be negative");
./ext/bz2/bz2.c: php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not read valid bz2 data from stream");
./ext/bz2/bz2.c: php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid mode for bzopen(). Only 'w' and 'r' are supported.", mode);
./ext/bz2/bz2.c: php_error_docref(NULL TSRMLS_CC, E_WARNING, "filename cannot be empty");
./ext/bz2/bz2.c: php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot use stream opened in mode '%s'", stream->mode);
./ext/bz2/bz2.c: php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot use stream opened in mode '%s'", stream->mode);
@gooh
gooh / xpath_match_all
Last active September 28, 2015 00:48
function for people whining that preg_match_all is less to type and concluding from it that regex must be better for parsing html than a dom parser
<?php
/**
* Run an XPath query against an HTML string and returns the results
*
* @param string $xpath The XPath query to run on $thml
* @param string $html The HTML to parse. Uses the previously used string if omitted
* @return array
*/
function xpath_match_all($query, $html = '')
{
@gooh
gooh / Dirty Casting
Last active December 13, 2015 18:39
Here be dragons and black magic!
<?php
class Caster
{
/**
* Casts an Array to an instance of a class
*
* This method will return an instance of the given $className with all
* elements in the array being public members of the instance. The method
* uses serialization to work, so no constructor will be called to get the
* instance of $className. The returned instance may behave unexpectedly
@gooh
gooh / Turn all internal functions into Closures
Last active December 13, 2015 18:39
Code to turn all defined internal functions into Closures to be able to pass them around as First Class Citizens. Not that you should do that.
<?php
$functions = get_defined_functions();
foreach($functions['internal'] as $function) {
$GLOBALS[$function] = function() use ($function) {
return call_user_func_array(
$function,
func_get_args()
);
};
@gooh
gooh / Attribute Reader
Created February 15, 2013 12:17
Just a proof of concept for an attribute reader trait
<?php
trait AttributeReader
{
/**
* Interceptor for non-accessible properties
*
* @param string $property
* @returns mixed
*/
public function __get($property)
<?php
for ($i = 1; $i <= 100; $i++) {
echo
$i % 3 === 0 ? 'Fizz' : '',
$i % 5 === 0 ? 'Buzz' : '',
$i % 3 && $i % 5 ? $i : '',
PHP_EOL;
};
@gooh
gooh / lint
Last active December 18, 2015 22:39
A simple linter for code snippets. Writes the snippet to a temp file and then captures and returns the output of php's commandline lint
<?php
function lint($code)
{
$code = strpos($code, '<?php') === 0 ? $code : "<?php $code";
$filename = tempnam(sys_get_temp_dir(), 'lnt');
file_put_contents($filename, $code);
exec(PHP_BINARY . " -l $filename", $errors);
unlink($filename);
@gooh
gooh / randomLineGenerator
Last active December 20, 2015 08:09
Retrieve the lines in a file in random order without repeating the lines or loading the file into memory first.
function getLinesInRandomOrder($filePath)
{
$file = new SplFileObject($filePath);
$file->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY);
$file->seek(PHP_INT_MAX);
$linesToTry = range(0, $file->key());
shuffle($linesToTry);
foreach ($linesToTry as $line) {
$file->seek($line);
yield $file->current();
### Keybase proof
I hereby claim:
* I am gooh on github.
* I am gooh (https://keybase.io/gooh) on keybase.
* I have a public key whose fingerprint is 729C 589F B492 0AE6 C22C 08F3 73BC 4368 C9EB 252E
To claim this, I am signing this object:
@gooh
gooh / gist:4b8f9eec685f0641834d
Created April 22, 2015 12:39
Universal Data Container
<?php
class DataCollection implements \IteratorAggregate
{
private $data;
public function __construct($data)
{
$this->data = $data;
}