Created
June 24, 2016 05:39
-
-
Save cvonkleist/a252694e5fb71b4046dbf1adb9682a93 to your computer and use it in GitHub Desktop.
Archon mixin system pseudocode
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 | |
// This class has functionality which can be used by many other classes. | |
class CommonFunctionalityClass { | |
function doSomethingCommon() { | |
$this->supportingFunction(); | |
} | |
function supportingFunction() { | |
echo(“hi”); | |
} | |
} | |
// This class wants the `doSomethingCommon` method, but it can’t extend | |
// CommonFunctionalityClass because it already extends SomeIrrelevantClass. | |
class ClassThatWantsCommonFunctionality extends SomeIrrelevantClass { | |
// PHP’s magic __call method, evoked when $method doesn’t exist. | |
function __call($method, $args) { | |
if ($method == “doSomethingCommon”) { | |
eval(“CommonFunctionalityClass::doSomethingCommon(“ . $args . “)”); | |
} | |
else { | |
// raise an error | |
} | |
} | |
} | |
// Now you can call the common functionality ... | |
$object = new ClassThatWantsCommonFunctionality(); | |
// ... and this nonexistent instance method is intercepted by PHP | |
// and handled with `__call`: | |
$object->doSomethingCommon(); | |
// Ultimately, this leads to the following code being eval’d: | |
CommonFunctionalityClass::doSomethingCommon(); | |
// NOTE: This is a simplification! | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment