php__phplib/misc/psr0.php

66 lines
1.9 KiB
PHP

<?php
error_reporting(-1);
/**
* Sample PSR-0 autoloader.
*
* Do not use as such: it is only present to demo classes in a PSR-0 context.
*
* Straight from the PSR-0 standard.
*
* @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
*
* @param string $className
*/
function psr0_autoload($className) {
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strripos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
/*
$stack = array_slice(debug_backtrace(), 2, 1);
$stack = reset($stack);
unset($stack['args']);
print_r($stack);
*/
$sts = @include $fileName;
return $sts;
}
/**
* An SPL autoloader function designed to avoid fatals on unfound classes.
*
* Add at the end of the autoloader chain and it will implement a stub class
* which will throw an exception, hence be catchable, when instantiated or used
* in a static call, instead of causing a PHP fatal error.
*
* Note that this has nothing to do with PSR0/PSR4, and PSR4 actually forbids
* compliant autoloated from throwing exceptions, so a function like this is
* actually needed when using a PSR4 autoloader, since the autoloated itself
* will not be allowed to catch the missing class.
*
* @param $name
* The name of a class to be loaded.
*/
function autoload_except($name) {
$src = <<<EOT
class $name {
public function __construct() {
throw new InvalidArgumentException("$name could not be found.");
}
public static function __callStatic(\$method_name, \$arguments) {
throw new InvalidArgumentException("$name could not be found.");
}
}
EOT;
eval($src);
}