Created
April 9, 2019 16:16
-
-
Save zonuexe/7febf0ae3a91ab8846a4914bd9694379 to your computer and use it in GitHub Desktop.
FizzBuzz loader
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 | |
trait Fizz { function __toString() { return "Fizz"; } } | |
trait Buzz { function __toString() { return "Buzz"; } } | |
trait FizzBuzz { function __toString() { return "FizzBuzz"; } } | |
trait N { function __toString() { return ltrim(__CLASS__, @_); } } | |
spl_autoload_register(function ($class) { | |
$n = ltrim($class, @_); | |
$trait = null; | |
if ($n % 3 === 0) $trait .= "Fizz"; | |
if ($n % 5 === 0) $trait .= "Buzz"; | |
$trait = $trait ?? 'N'; | |
eval("final class $class { use $trait; }"); | |
}); | |
foreach (range(1, 100) as $n) { | |
$class = "_$n"; | |
echo new $class, PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment