Skip to content

Instantly share code, notes, and snippets.

@zonuexe
Created April 9, 2019 16:16
Show Gist options
  • Save zonuexe/7febf0ae3a91ab8846a4914bd9694379 to your computer and use it in GitHub Desktop.
Save zonuexe/7febf0ae3a91ab8846a4914bd9694379 to your computer and use it in GitHub Desktop.
FizzBuzz loader
<?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