Hi, Iā€™m Troy.

I build websites.

Studying

Want a crash course in some high-ish level programming concepts, written in the most technically boring and hard-to-follow language possible? Then you’ve come to the right place.

Class Abstraction

An abstract class is basically a blueprint for creating another class. Abstract classes cannot be instantiated, but instead must be extended by another class. An abstract class can define properties and methods that will come with the new class that extends it, and can also define abstract properties and methods that the developer must define within the extending class.

Abstract classes should be designed to be filled in by extending classes. A simple example would be an abstract class Human that has an abstract property gender, with extending classes Male and Female that automatically set the gender property from the abstract class.

Object Interfaces

An interface looks a lot like an abstract class, and the two can be easily confused because the basic idea is the same. They both provide a blueprint for other classes to extend upon, but an interface is more strict.

The idea of an interface is to provide a contract for an extending class to create methods that implement the methods defined in the interface. An interface is usually used to describe a protocol that an implementing class must follow.

Factory Design Pattern

The Factory Design Pattern is a way to structure your object-oriented system so that it allows polymorphism for objects that may differ based on certain inputs. An interface is defined that enforces a list of methods that an implementing class must adhere to, and a factory class is responsible for evaluating properties and returning the proper object based on those properties.

The following code fully implements the factory design pattern, creating an interface called IForm, implementing that interface with an abstract class HumanForm, and extending that abstract class with MaleForm and FemaleForm. The HumanFormFactory returns the correct object depending on the first property given to the factory object.

<?php
interface IForm {
  public function submit();
}

abstract class HumanForm implements IForm {
  protected $name;
  protected $email;
  protected $gender;
  protected $voice;

  public function __construct($name, $email) {
    $this->name = $name;
    $this->email = $email;
  }

  public function submit() {
    /* stuff to submit the form */
  }
}

class MaleForm extends HumanForm {
  public function __construct($name, $email) {
    $this->gender = 'male';
    $this->voice = 'low';

    parent::__construct($name, $email);
  }
}

class FemaleForm extends HumanForm {
  public function __construct($name, $email) {
    $this->gender = 'female';
    $this->voice = 'high';

    parent::__construct($name, $email);
  }
}

class HumanFormFactory {
  static public function create($gender, $name, $email) {
    switch($gender) {
      case 'male':
        return new MaleForm($name, $email);
      case 'female':
        return new FemaleForm($name, $email);
      default:
        throw new Exception('Invalid gender');
    }
  }
}

$form = HumanFormFactory::create('male', 'Troy', 'troyswanson@me.com');
/* Sets $form to a MaleForm object */

Closures

A closure is a function that references a variable outside of its local scope that would have normally been removed from memory after its encapsulating function has exited.

For example, if the function foo is defined inside the function bar, and references the variable x that was also defined inside bar, and the foo function is returned by bar back into the global scope, then the variable x is considered to be closed around the context of foo. This creates a closure which prevents x from being destroyed when bar exits, and allows foo to be run in the global scope while still referencing x.

function bar(num) {
  var x = num;
  function foo() {
    return x * 2;
  }
  return foo;
}

var y = bar(5)();
var z = bar(7)();

alert(z); // 14
alert(y); // 10

This snippet demonstrates the behavior described above, but also illustrates the idea that, since functions are so portable in JavaScript, you can execute the function that is returned by another function immediately, and then store the value that it returns into another variable. The exercise of executing a function that is returned by another function is referred to as an Immediately Invoked Function Expression.