Skip to content

Instantly share code, notes, and snippets.

View Jeff-Russ's full-sized avatar

Jeffrey Russ Jeff-Russ

View GitHub Profile
@Jeff-Russ
Jeff-Russ / PHP: Static Method Chaining.md
Last active January 4, 2017 09:09
PHP: Static Method Chaining
<?php
class One {
    static function echoGetClass() { echo get_class()."\n"; }
    
    private static $it;
    # chaining is possible if you return the class name! ...
    static function setIt($it)  { One::$it = $it; return 'One';}
    static function echoIt()    { echo One::$it;  return 'One';}
    
@Jeff-Russ
Jeff-Russ / Nil.php.md
Last active December 31, 2016 08:16
PHP: Nil Exception and NIL constant

Nil Exception and NIL constant

<?php
define('NIL', '');

class Nil extends Exception {

    final public function __toString() { return NIL; }
}
@Jeff-Russ
Jeff-Russ / PHP Performance: if,elseif vs case,switch vs nested if.php
Created December 17, 2016 03:00
PHP Performance: if/elseif vs case/switch vs nested if
<?php
$iter = 500000;
$start = microtime(true);
$n = 0;
for ($c=0; $c<7*$iter; $c++) {
$i = $c % 16;
if ($i===0) $n += 0;
<?php
/**
* Node is a smart array object inspired by the Node/Tree data structure and the
* UNIX file system's inodes. Just a UNIX directory has a hidden file called
* '..' in every directory which is a reference to the parent directory, a Node
* object has $node['..'] which is a reference to the parent Node, or itself
* if there is no parent Node meaning the Node is 'root', and it's $node['/']
* element which would also be reference to itself if root.
*
* Basically a Node is an array on steroids but why would you need this? Nodes
@Jeff-Russ
Jeff-Russ / Array Objects in PHP.md
Last active April 5, 2024 21:07
PHP: ArrayObject, IteratorAggregate , ArrayAccess , Serializable , Countable

Array Objects in PHP

Array are not object in PHP but PHP does give us some ways to make object that act like arrays. First, there is the ArrayObject class which is pretty close to what you have with a normal array (you can iterate it, use [] etc.) If you use it as a parent for your class you can add methods to it.

class ArrObj extends ArrayObject{
	// add methods here
@Jeff-Russ
Jeff-Russ / PHP: Recursively Iterate All Nodes.md
Last active December 8, 2016 18:28
PHP: Recursively Iterate All Nodes

Recursively Iterating All Nodes in PHP

SEE UPDATE BELOW

First let's make our own sort of non-recursive map function:

function map (&$array, $callback)
{
    foreach ($array as $key => &$value) {

Zenity Helper Functions

Zenity is great to give your Bash scripts a nice, albiet minimal, graphical interface. If you are the type of programmer that likes to stick by the 80 character per line rule you'll likely find using it annoying as it involoves some very long statements. On top of that, the commands and switches are descriptive but not quick to type. A lot of it is unavoidable but some is not.

zenity --file-selection --save --confirm-overwrite --title "Your Title"
@Jeff-Russ
Jeff-Russ / Interacting with Shell Parameters in Ruby.md
Last active October 25, 2016 19:07
Interacting with Shell Parameters in Ruby

Interacting with Shell Parameters in Ruby

Shell Parameters vs Other Languages

The term "Parameters" in the context of a shell command line or shell script differs from what it means for functions and methods in other programming languages. In most programming languages, each parameter is separated from the next with a comma whereas in the shell spaces are typically what delimits the parameters, which can be overridden with double quotes.

In Ruby and in the Shell, once you assign or pass a string with double quotes the double quotes are used to indicate the beginning and end but do not become part of the actual eventual value. Take this Ruby script as an example:

arg1 = "one"
#!/usr/bin/env bash
# BAShIC - BASIC interpreter written in Bash
# Based on bournebasic by Brandon S. Allbery 1987
#
# 10 print "Enter any number and I will double it for you ";
# 20 input x
# 30 y = 2*x
# 40 print "The answer is ";
# 50 print y
# 60 print "Enter -1 if you want to continue."