Skip to content

Instantly share code, notes, and snippets.

View amacgregor's full-sized avatar
:shipit:
Working on Side Projects

Allan MacGregor amacgregor

:shipit:
Working on Side Projects
View GitHub Profile
@amacgregor
amacgregor / Product.php
Last active January 2, 2016 08:09
Example Product class for without dependency injection
<?php
class Product {
private $stockItem;
private $sku;
public function __construct($sku, $stockQuantity, $stockStatus){
$this->stockItem = new StockItem($stockQuantity, $stockStatus);
$this->sku = $sku;
}
@amacgregor
amacgregor / StockItem.php
Last active January 20, 2019 20:36
Example Stock Item class without dependency injection
<?php
class StockItem {
private $quantity;
private $status;
public function __construct($quantity, $status){
$this->quantity = $quantity;
$this->status = $status;
@amacgregor
amacgregor / Product.php
Created January 5, 2014 23:46
Example Product class for using constructor injection
<?php
class Product {
private $stockItem;
private $sku;
public function __construct($sku, StockItem $stockItem){
$this->stockItem = $stockItem;
$this->sku = $sku;
}
@amacgregor
amacgregor / Product.php
Created January 5, 2014 23:55
Example Product class for using setter injection.
<?php
class Product {
private $stockItem;
private $sku;
public function __construct($sku){
$this->sku = $sku;
}
@amacgregor
amacgregor / productClasses.php
Last active January 3, 2016 19:09
Example for Design Patterns in PHP: Using Factories
<?php
abstract class Product
{
private $sku;
private $name;
protected $type = null;
public function __construct($sku, $name)
{
@amacgregor
amacgregor / factories1.php
Created January 19, 2014 16:05
Example calls for product
<?php
$product = new Product_Chair('0001','INGOLF Chair');
...
$product = new Product_Table('0002','STOCKHOLN Table');
...
@amacgregor
amacgregor / ProductController.php
Last active January 3, 2016 19:19
Product controller updated with factory
<?php
class ProductController {
public function create($product_type)
{
// Some post data validation logic here
// Now we need to instantiate our product
$product = ProductFactory::build($product_type, $post['sku'], $post['name'])
@amacgregor
amacgregor / productFactory.php
Last active January 3, 2016 19:19
Product Factory
<?php
class ProductFactory
{
public static function build($product_type, $sku, $name)
{
$product = "Product_" . ucwords($product_type);
if(class_exists($product))
{
return new $product($sku, $name);
@amacgregor
amacgregor / UserSingleton.php
Last active March 8, 2024 07:25
PHP Singleton pattern example
<?php
/** Example taken from http://www.webgeekly.com/tutorials/php/how-to-create-a-singleton-class-in-php/ **/
class User
{
// Hold an instance of the class
private static $instance;
// The singleton method
@amacgregor
amacgregor / instructions_hhvm
Created February 9, 2014 11:54
HVVM Installation Instructions
mkdir dev
cd dev
export CMAKE_PREFIX_PATH=`pwd`
git clone git://github.com/facebook/hhvm.git
cd hhvm
git submodule init
cd ../
## Required for builtin web-server support