Skip to content

Instantly share code, notes, and snippets.

@imliam
imliam / safe.php
Last active January 11, 2018 17:13
Safely attempt to call methods on something you believe to be an object.
<?php
if (! function_exists('safe')) {
/**
* Safely attempt to call methods and properties on something you believe
* to be an object but may be null, using a hidden anonymous class for
* syntactical sugar to keep your application logic looking simple
* and clean.
*
* @param object $value The suspected object.
@imliam
imliam / bind_methods.php
Created January 10, 2018 18:10
Trait to dynamically bind methods to a class.
<?php
trait BindMethods
{
private $boundMethods = [];
public function bindMethod($methodName, $method) {
$this->boundMethods[$methodName] = Closure::bind($method, $this, get_class());
}
<?php
/**
* Allows a method in a class to be called either statically or instanced.
*
* To use, ensure custom method names are camelCase starting withthe word
* "either". For example, a method defined as "eitherGetResults()"
* can be called in either of the two following ways:
*
* $exampleObject->getResults()
@imliam
imliam / take.php
Last active January 15, 2020 14:30
Run functions consecutively by piping through the result of one into the next.
<?php
if (! function_exists('take')) {
/**
* Run functions consecutively by piping through the result of one
* into the next.
*
* @param mixed $value A value
*
* @return object
*/
<?php
if (! function_exists('catch_exit')) {
/**
* Catch when a part of a script exits and execute a custom function at
* that point. Note that if exiting, the script does not continue.
* You can use this to, for example, flash a message and
* redirect the user instead of showing the default
* black text on white background "exit" view.
*
<?php
define('PIPE_VALUE', '__pipe-' . uniqid());
class Pipe implements ArrayAccess, Iterator, Serializable
{
public $value;
private $position = 0;
public function __construct($value)
@imliam
imliam / operator-mono-lig.css
Created July 5, 2018 12:37
Use the Operator Mono Lig typeface on any website.
/** General websites **/
code { font-family: "Operator Mono Lig" !important; font-weight: 200; }
pre > code { font-family: "Operator Mono Lig" !important; font-size: 1.2em !important; font-weight: 200; }
/** GitHub **/
.blob-code-inner, .blob-num, .highlight pre { font-family: "Operator Mono Lig" !important; font-weight: 200; }
.pl-c, .pl-e { font-style: italic; }
.pl-c { color: #4CAF50; }
/** Prism JS **/
@imliam
imliam / sizing-helpers.scss
Created August 9, 2018 23:20
Utilities for controlling an element's padding and margin.
/*
|--------------------------------------------------------------------------
| Spacing
|--------------------------------------------------------------------------
|
| Utilities for controlling an element's padding and margin.
|
*/
$spacing-class: (
@imliam
imliam / example.php
Created August 23, 2018 20:17
Define a set of constants to be used as flags for bitmasked options.
<?php
set_bitmask_flags([
'FLAG_1',
'FLAG_2',
'FLAG_3',
'FLAG_4',
'FLAG_5',
]);
-- Weigh rows against eachother based on different conditions,
-- ordering the results based on their given weights so that
-- more precise matches will show higher up in the results.
-- In this example, an exact match will show up at the top
-- of the results, a match at the beginning of the string
-- will show next, and a match anywhere will show last.
set @query = 'Liam';