Factory Pattern

<?php
interface GirlInterface{
function get_name();
function get_hair_color();
}

class Girl implements GirlInterface{
protected $_name,$_hair_color;
public function __construct($name,$hair_color){
$this->_name = $name;
$this->_hair_color = $hair_color;
}
public function get_name(){
return $this->_name;
}
public function get_hair_color(){
return $this->_hair_color;
}
}

class GirlFactory{
public static function Create($name,$hair_color){
return new Girl($name,$hair_color);
}
}

echo '~~~~~~~~~~~~~ Factory Example: ~~~~~~~~~~~~~';
$gf = GirlFactory::Create('Tanya','Red');
echo "Girl's name: {$gf->get_name()}<br>";
echo "Girl's hair: {$gf->get_hair_color()}<br><br>";
?>

Strategy Pattern

<?php
/* ~~~~~~~~~~~~~ Car Repair Classes ~~~~~~~~~~~~~ */

abstract class CarRepair{
protected $_duration,$_cost_calculator;
public function __construct($duration, CostCalculator $cost_calculator){
$this->_duration = $duration;
$this->_cost_calculator = $cost_calculator;
}
public function get_cost(){
return $this->_cost_calculator->get_cost($this);
}
public function get_charge_type(){
return $this->_cost_calculator->get_charge_type($this);
}
public function get_duration(){
return $this->_duration;
}
}

class OilChange extends CarRepair{
public function __construct($duration, CostCalculator $cost_calculator){
parent::__construct($duration,$cost_calculator);
}
//Overwrite Parent Method
public function get_cost(){
//For overwrite example only - calcs should be kept in the CostCalculator Classes
return $this->_cost_calculator->get_cost($this) * 2;
}
//Specific Method
public function get_job_type(){
return 'Oil Change';
}

}

class TireRotation extends CarRepair{
public function __construct($duration, CostCalculator $cost_calculator){
parent::__construct($duration,$cost_calculator);
}
//Specific Method
public function get_job_type(){
return 'Tire Rotation';
}
}

/* ~~~~~~~~~~~~~ Cost Calculators ~~~~~~~~~~~~~ */

abstract class CostCalculator{
abstract function get_cost(CarRepair $car_repair);
abstract function get_charge_type();
}

class HourlyCostCalculator extends CostCalculator{
public function get_cost(CarRepair $car_repair){
return($car_repair->get_duration() * 30);// $30 an hour
}
public function get_charge_type(){
return '$30 Hourly Rate';
}
}

class DiscountHourlyCostCalculator extends CostCalculator{
public function get_cost(CarRepair $car_repair){
return($car_repair->get_duration() * 20);// $20 an hour
}
public function get_charge_type(){
return '$20 Discount Hourly Rate';
}
}

//Print Example
echo '~~~~~~~~~~~~~ Strategy Example: ~~~~~~~~~~~~~';

$jobs[] = new OilChange(4, new HourlyCostCalculator());
$jobs[] = new TireRotation(4, new DiscountHourlyCostCalculator());

foreach($jobs as $job){
echo $job->get_job_type() . ':<br>';
echo 'Job Charge: $' . $job->get_cost() . '<br>';
echo 'Charge Type: ' . $job->get_charge_type() . '<br><br>';
}
?>

Parent Body Class

<?php

function top_level_parent_class($classes) {
global $post;
if (is_page() && !$post->post_parent && !is_front_page()) {
$classes[] = 'page-top-parent';
}
return $classes;
}
add_filter('body_class', 'top_level_parent_class');

?>

Grandparent

<?php

function get_grandparent($page_id) {
$current_page = get_page($page_id);
if ($current_page->post_parent > 0) {
$parent_page = get_page($current_page->post_parent);
if ($parent_page->post_parent > 0) {
return $parent_page->post_parent;
} else {
return false;
}
}
return false;
}

function grandparent_id_class($classes) {
global $post;
if (is_page() && $post->post_parent && !is_front_page() && get_grandparent($post->ID)) {
$grandparent = get_grandparent($post->ID);
$classes[] = 'grandparent-pageid-'. $grandparent .'';
}
return $classes;
}
add_filter('body_class', 'grandparent_id_class');

?>

Get Interceptor

class GetInterceptor{
public function __get($property){
$method = "get_{$property}";
if(method_exists($this,$method)) return $this->$method();
}
public function get_hello_text(){
return 'Hello';
}
}

$get_interceptor = new GetInterceptor();
echo $get_interceptor->hello_text;