Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.
For the best experience please use the latest Chrome, Safari or Firefox browser.
Use a spacebar or arrow keys to navigate
Presented by Ben Doherty @ Oomph, Inc.
Email: ben@oomphinc.com
Skype: bdoherty.oomphinc
Github: http://github.com/bendoh/
Located right here in sunny Providence, Rhode Island!
Chunks of functionality become distinct pieces
Just re-name a class (and maybe actions / filters) and it's ready for another theme.
All related code is tucked away in a single place.
// These two features are available as of PHP 5.3, // but WordPress can run on PHP 5.2.4 // Cool new operator... Short-circuit ternary? $the_value = $_POST['value'] ?: "default value"; // Anonymous functions. add_filter( 'the_content', function( $content ) { return $content . " filtered!"; });
theme/functions.php
theme/functions/
theme/functions/template.php
theme/functions/class-*.php
theme/functions/class-my-sweet-post.php
wp-config.php
:define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', false );
define( 'WP_DEBUG_LOG', true );
wp-content/debug.log
wp-content/debug.log
like a HAWK! PHP Warnings are a bad thing and can indicate bugs in your code.class My_Singleton { static function init() { add_action( 'save_post', array( get_class(), 'action_save_post' ) ); } static function action_save_post( $post_id ) { ... } } My_Singleton::init();
$this
);class My_Singleton { private function __construct() { add_action( 'save_post', array( $this, 'action_save_post' ) ); } private function __clone() { } // Disallow clone() of object private static $instance = false; public static function get_instance() { if( !$self::instance ) $self::instance = new My_Singleton; return $self::instance; } static function action_save_post( $post_id ) { ... } } My_Singleton::get_instance();
class Singleton { protected static $instances; protected function __construct() { } final private function __clone() { } public static function get_instance() { $class = get_called_class(); if (!isset(self::$instances[$class])) { self::$instances[$class] = new $class; } return self::$instances[$class]; } }
class My_Singleton extends Singleton { function My_Singleton() { add_action( 'save_post', array( $this, 'action_save_post' ) ); } function action_save_post( $post_id ) { ... } } My_Singleton::get_instance();
get_called_class()
)function my_singleton_factory( $class ) { static $instances; if( !isset( $instances[$class] ) ) $instances[$class] = new $class; return $instances[$class]; }
/* * Placeholder class that simulates a real object, but does nothing. * Code depending on a missing class won't cause fatal errors, * Messages will get printed to error log when accessed */ class My_Placeholder { private $placeholder_class = ''; private $reason = ''; function __construct( $class, $reason ) { $this->placeholder_class = $class; $this->reason = $reason; error_log( "Attempted to create non-existent class singleton in class '$this->placeholder_class' (reason=$reason)" ); } function __call( $method, $args ) { error_log( "Attempted to call method '$method' on missing class '$this->placeholder_class'; args", $args ); return null; } function __get( $name ) { error_log( "Attempted to access object property '$name' on missing class '$this->placeholder_class'" ); return null; } function __set( $name, $value ) { error_log( "Attempted to set object property '$name' to '$value' on missing class '$this->placeholder_class'" ); } }
function my_singleton_factory( $class ) { static $instances; if( !class_exists( $class ) ) return new My_Placeholder( $class, "Class does not exist" ); if( !isset( $instances[$class] ) ) $instances[$class] = new $class; return $instances[$class]; }
UNLESS the classes are very tightly related and co-dependant
functions.php
, instantiate separately:include( __DIR__ . '/functions/class-my-sweet-post.php' );
new My_Sweet_Post;
My_Sweet_Post::get_instance();
my_singleton_factory( 'My_Sweet_Post' );
functions.php
gives you more control over whether you want the featured enabled.functions/class-my-event.php
single-event.php
functions/class-my-event.php
single-event.php
$options
structure