Skip to content

Instantly share code, notes, and snippets.

View adrian-enspired's full-sized avatar

Adrian adrian-enspired

View GitHub Profile
@adrian-enspired
adrian-enspired / jquery.AT.autoXHR.js
Created September 19, 2012 04:51
a jQuery (v1.8.1) plugin which uses data-* attributes to unobtrusively trigger ajax calls.
/**
* @package javascriptApp
* @author Adrian Testa-Avila <[email protected]>
* @copyright 2012 Adrian Testa-Avila
* @license Creative Commons Attribution-ShareAlike 3.0 Unported
* <http://creativecommons.org/licenses/by-sa/3.0/>
*/
/**
* jQuery plugin: AT.autoXHR
@adrian-enspired
adrian-enspired / mVc.php
Created October 19, 2012 03:06
PHP-FIRST: a beginning at separating the view from the controller & model
<?php
/*
Not trying to be a complete MVC implementation, obviously,
just a good, easy-to-implement introduction for the average PHP coder/hobbyist.
It WILL make your scripts BETTER! :)
The basic rule is "PHP-FIRST":
meaning, don't end php ?>, do some html, and then start <?php again.
PHP doesn't *actually* work this way,
(you can't "start," "stop," and "start again": breaking into HTML is pretty much the same as echo'ing it)
@adrian-enspired
adrian-enspired / bitmask.php
Last active December 10, 2015 02:48
A utility class to create, manipulate, and check bitmasks (use powers of 2 for your values!). Written and tested with php5.4; should be compatible with 5.1+. Exception-based error handling. Released under the MIT license. See example usage below class definition.
<?php
/**
* models a bitfield and provides manipulation & comparison methods.
*
* @author Adrian Testa-Avila <[email protected]>
* @copyright 2012
* @license MIT License [http://opensource.org/licenses/MIT]
*/
class Bitfield{
@adrian-enspired
adrian-enspired / twitter_user.php
Last active July 7, 2018 20:31
a class to access info from Twitter's "Sign in With Twitter" API. You can add additional methods as desired. See the conversation here: [http://css-tricks.com/forums/discussion/21825/output-value-of-nested-arrays]
<?php
/**
* wraps info returned from twitter's "sign in with twitter" API.
* add additional methods/properties as desired and use as a user class.
*
* @author Adrian Testa-Avila <[email protected]>
* @copyright 2013 Adrian Testa-Avila
* @license creative commons attribution-sharealike
* http://creativecommons.org/licenses/by-sa/3.0/
*
@adrian-enspired
adrian-enspired / die.php
Created March 3, 2013 06:12
error handling commentary.
<?php
die(); # WHY WONT IT
<?php
// you can't just get this as JSON in the first place, can you ...?
$xml = simplexml_load_file( "http://readability.com/christopherburton/latest/feed" );
$json = json_encode( $xml );
$array = json_decode( $json,TRUE );
$items = $array['channel']['item'];
// we're doing this now so we can sanitize the data without requiring a second loop
// (substitute your actual DB credentials)
$DB = new mysqli( DB_HOST,DB_USER,DB_PASS,DB_NAME );
<?php
/*
You don't need ReflectionClass at all - just make an interface for your classes to follow.
Define all the methods that you require of your classes for this situation.
*/
interface iA{
// (modified init signature to include args you pass in your example)
public function init( $request,$response );
@adrian-enspired
adrian-enspired / is.php
Last active August 29, 2015 14:02
convenience function for ?: short ternary assignment syntax; avoids need for isset() checks.
<?php
/**
* performs validation on a variable, returning the variable's value on success.
*
* @author Adrian <[email protected]>
* @copyright 2013
* @since 2014-08-21 allows a validator callback
* @license MIT <http://opensource.org/licenses/MIT>
*
<?= strip_tags('I <3 WordPress') ?>
@adrian-enspired
adrian-enspired / post-defaults-concept.php
Last active January 1, 2021 03:13
I don't actually recommend this approach anymore. you're better off with a more holistic approach to input validation.
<?php
// I first saw this on freenode/##php, from Viper-7.
// first, make an array with ALL of the field names you expect, and default values for each.
// keys are field names; values are field defaults.
$defaults = [
"field1" => "default value",
"field2" => "", // ← default is empty string
"field3" => null // ← no default value