psr0.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. error_reporting(-1);
  3. /**
  4. * Sample PSR-0 autoloader.
  5. *
  6. * Do not use as such: it is only present to demo classes in a PSR-0 context.
  7. *
  8. * Straight from the PSR-0 standard.
  9. *
  10. * @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
  11. *
  12. * @param string $className
  13. */
  14. function psr0_autoload($className) {
  15. $className = ltrim($className, '\\');
  16. $fileName = '';
  17. $namespace = '';
  18. if ($lastNsPos = strripos($className, '\\')) {
  19. $namespace = substr($className, 0, $lastNsPos);
  20. $className = substr($className, $lastNsPos + 1);
  21. $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
  22. }
  23. $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
  24. /*
  25. $stack = array_slice(debug_backtrace(), 2, 1);
  26. $stack = reset($stack);
  27. unset($stack['args']);
  28. print_r($stack);
  29. */
  30. $sts = @include $fileName;
  31. return $sts;
  32. }
  33. /**
  34. * An SPL autoloader function designed to avoid fatals on unfound classes.
  35. *
  36. * Add at the end of the autoloader chain and it will implement a stub class
  37. * which will throw an exception, hence be catchable, when instantiated or used
  38. * in a static call, instead of causing a PHP fatal error.
  39. *
  40. * Note that this has nothing to do with PSR0/PSR4, and PSR4 actually forbids
  41. * compliant autoloated from throwing exceptions, so a function like this is
  42. * actually needed when using a PSR4 autoloader, since the autoloated itself
  43. * will not be allowed to catch the missing class.
  44. *
  45. * @param $name
  46. * The name of a class to be loaded.
  47. */
  48. function autoload_except($name) {
  49. $src = <<<EOT
  50. class $name {
  51. public function __construct() {
  52. throw new InvalidArgumentException("$name could not be found.");
  53. }
  54. public static function __callStatic(\$method_name, \$arguments) {
  55. throw new InvalidArgumentException("$name could not be found.");
  56. }
  57. }
  58. EOT;
  59. eval($src);
  60. }