Created
April 26, 2012 14:51
-
-
Save damonsk/2500129 to your computer and use it in GitHub Desktop.
ClassController
This file contains 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 | |
/** | |
* -------------------------------------------------------------------- | |
* @authour: Damon Skelhorn <[email protected]> Twitter: @damonsk; | |
* @date: 26/04/2012 | |
* -------------------------------------------------------------------- | |
* | |
* Usage: | |
* | |
* ClassController::includeClass('MyClass'); | |
* | |
* 1. Create a class with name MyClassCore with filename MyClass.php | |
* -- This class should contain the core functionality. | |
* | |
* 2. Create a class MyClass that extends MyClassCore in subfolder overrides with filename MyClass.php | |
* -- This will contain functionality that will to override the core functionality. | |
* | |
* 3. MyClass constructor should call super on MyClassCore. e.g: parent::__construct(); | |
* | |
* Useful if you use source control and want to extend functionality without changing the code base for | |
* individual projects. Regardless if a overriding class exists, you call MyClass and not MyClassCore. | |
* | |
*/ | |
class ClassController | |
{ | |
public static function includeClass($className) | |
{ | |
if (!class_exists($className, false)) { | |
require_once(dirname(__FILE__) . '/' . $className . '.php'); | |
if (file_exists(dirname(__FILE__) . '/overrides/' . $className . '.php')) | |
require_once(dirname(__FILE__) . '/overrides/' . $className . '.php'); | |
else | |
{ | |
$coreClass = new ReflectionClass($className . 'Core'); | |
if ($coreClass->isAbstract()) | |
eval('abstract class ' . $className . ' extends ' . $className . 'Core {}'); | |
else | |
eval('class ' . $className . ' extends ' . $className . 'Core {}'); | |
} | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment