Skip to content

Instantly share code, notes, and snippets.

@cvonkleist
Created June 24, 2016 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvonkleist/a252694e5fb71b4046dbf1adb9682a93 to your computer and use it in GitHub Desktop.
Save cvonkleist/a252694e5fb71b4046dbf1adb9682a93 to your computer and use it in GitHub Desktop.
Archon mixin system pseudocode
<?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