Skip to content

Instantly share code, notes, and snippets.

View johnkary's full-sized avatar

John Kary johnkary

View GitHub Profile
@johnkary
johnkary / method_missing-in-php.php
Last active February 14, 2019 20:06
Example of meta-programming in PHP 5.6+ using Ruby's method_missing concept. See in action here: https://3v4l.org/pfWTh
<?php
class Sample
{
private $name;
// Name is required!
public function __construct($name) {
$this->setName($name);
}
@johnkary
johnkary / commit-msg
Created November 29, 2018 14:47
Search git repo for JIRA ticket numbers found in commit branch and message
#!/usr/bin/env bash
# Abort script if any command encounters an error
set -e
current_branch="$(git rev-parse --abbrev-ref HEAD)"
repo_root="$(git rev-parse --show-toplevel)"
# Get dir where this script lives
DIR="$( cd "$( dirname "$0" )" && pwd )"
@johnkary
johnkary / iterm_open_with
Last active January 22, 2020 13:02 — forked from peterjaap/iterm_open_with
Open text files from Iterm2 in PhpStorm by command+clicking on it
#!/bin/sh
# Open text files from Iterm2 in PhpStorm by command+clicking on it
# You will need the Remote call plugin in PhpStorm - https://plugins.jetbrains.com/plugin/6027
# And of course curl
# wget this and chmod +x it
# then move it to somwhere convenient in your path (such as /usr/local/bin)
# With respects to https://gist.github.com/trinitronx/f59a8308d42d71fdba41 for the basis for this
# iterm_open_with - open a URL, file from CWD, full path, or path with linenumber in default app or PhpStorm if text file
@johnkary
johnkary / variadic.php
Created May 11, 2016 03:23
How did PHP support variadic arguments before the `...$array` function argument syntax in PHP 5.6?
<?php
function some_old($fn) {
// Must remove $fn to use remaining args as variadic
$variadicArrays = array_slice(func_get_args(), 1);
// $variadicArrays is an "implicit" argument because it's not defined in the signature
var_dump($variadicArrays);
}
@johnkary
johnkary / xray.php
Last active November 18, 2022 23:58
Exposing protected and private object methods using \Closure::bindTo(). Code below requires PHP 5.6+ due to argument unpacking (...$args). Could probably be written for PHP 5.4+ but didn't want to reinvent argument unpacking, else could also just use the Reflection implementation.
<?php
class User {
private $firstname;
private $lastname;
public function __construct($f, $l) {
$this->firstname = $f;
$this->lastname = $l;
}
@johnkary
johnkary / fizzbuzz.php
Created May 7, 2015 04:11
Kansas City PHP User Group - May 6, 2015 - FizzBuzz without PHP keywords if, else, for, foreach, do, while, switch, and ternary operator.
<?php
$range = range(1,100);
$numbers = array_filter($range, function ($num) {
return $num % 3 !== 0 && $num % 5 !== 0;
});
$fizz = array_filter($range, function ($num) {
return $num % 3 === 0 && $num % 5 !== 0;
});
$buzz = array_filter($range, function ($num) {
@johnkary
johnkary / gist:339f16638929b6d9ace7
Last active August 29, 2015 14:20
Kansas City PHP User Group - Code Exercises

KC PHP User Group -- Code Exercises

  1. Install PHP, optionally with MySQL/Apache:
  2. Install Composer -- https://getcomposer.org/
  3. Choose an example problem from the below list you want to work on.
  4. Use libraries you've never used before!
  5. Regroup at the end to discuss what you tried and learned.
@johnkary
johnkary / the-most-common-way.php
Last active August 29, 2015 14:16
Example of using tuples instead of array key-value pairs
class BuildReport
{
private function getColumnHeaders()
{
// Why do you need array keys if this array is private details?
// It's never exposed outside this class so you have full control of its format
return [
['id' => 'first_name', 'label' => 'First Name'],
['id' => 'last_name', 'label' => 'Last Name'],
['id' => 'department', 'label' => 'Department'],
@johnkary
johnkary / mailer.php
Last active August 29, 2015 14:14
http://3v4l.org/tTreF -- PHP 5.6 example of "Functional Options" as presented by Rob Pike in his article "Self-referential functions and the design of options" http://commandcenter.blogspot.com.au/2014/01/self-referential-functions-and-design.html
<?php
/**
* PHP 5.6 example of "Functional Options" as presented by Rob Pike
* in his article "Self-referential functions and the design of options"
* http://commandcenter.blogspot.com.au/2014/01/self-referential-functions-and-design.html
*/
namespace Mailer {
class Mailer {
@johnkary
johnkary / MonologErrorEmailPrototype.php
Created January 20, 2015 16:29
How to customize Swift_Message creation via Monolog SwiftMailerHandler. See discussion https://github.com/Seldaek/monolog/pull/497
<?php
namespace My;
class MonologErrorEmailPrototype
{
private $fromAddress;
private $toAddress;
private $host;
private $env;