Last active
December 20, 2015 14:19
-
-
Save peterwilsoncc/6145280 to your computer and use it in GitHub Desktop.
Experimenting with singletons for WordPress themes - it makes it easier than dozens of calls to function_exists(), I think. Maybe.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Child Theme's functions.php */ | |
function collaborate_class_init() { | |
class Collaborate extends Collaborate_Parent_Theme { | |
public static function theme() { | |
if ( ! Collaborate::$instance instanceof self ) { | |
Collaborate::$instance = new self(); | |
} | |
return Collaborate::$instance; | |
} | |
protected function __construct() { | |
parent::__construct(); | |
// init any child specific stuff here | |
} | |
public function testing_un_dos_tres() { | |
echo 'child'; | |
} | |
} | |
} | |
/* Parent Theme's functions.php */ | |
class Collaborate_Parent_Theme { | |
protected static $instance; | |
public static function theme() { | |
if ( ! Collaborate_Parent_Theme::$instance instanceof self ) { | |
Collaborate_Parent_Theme::$instance = new self(); | |
} | |
return Collaborate_Parent_Theme::$instance; | |
} | |
protected function __construct() { | |
// init any theme stuff here | |
} | |
public function testing_un_dos_tres() { | |
echo 'parent'; | |
} | |
} | |
if ( ! function_exists( 'collaborate_class_init' ) ) { | |
function collaborate_class_init() { | |
class Collaborate extends Collaborate_Parent_Theme { | |
public static function theme() { | |
if ( ! Collaborate::$instance instanceof self ) { | |
Collaborate::$instance = new self(); | |
} | |
return Collaborate::$instance; | |
} | |
} | |
} | |
} | |
/* Testing shit */ | |
collaborate_class_init(); | |
Collaborate::theme(); | |
function scope_testing() { | |
Collaborate::theme(); | |
Collaborate::theme()->testing_un_dos_tres(); | |
} | |
scope_testing(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment