由于 OP 要求它,这是使用自动加载的更新答案。
注意:当然,为了简化任务,不需要如此复杂的结构。然而,这只是一个例子(自动加载、需求、静态方法等)。
引导/自动加载
/var/www/somedir/Twiggled/bootstrap.php
<?php
namespace Twiggled;
require_once __DIR__ . '\Common\AutoLoader.php';
$autoloader = new \Twiggled\Common\AutoLoader(__NAMESPACE__, dirname(__DIR__));
$autoloader->register();
/var/www/somedir/Twiggled/Common/AutoLoader.php
<?php
namespace Twiggled\Common;
/**
* PSR-0 Autoloader
*
* @package Common
*/
class AutoLoader
{
/**
* @var string The namespace prefix for this instance.
*/
protected $namespace = '';
/**
* @var string The filesystem prefix to use for this instance
*/
protected $path = '';
/**
* Build the instance of the autoloader
*
* @param string $namespace The prefixed namespace this instance will load
* @param string $path The filesystem path to the root of the namespace
*/
public function __construct($namespace, $path)
{
$this->namespace = ltrim($namespace, '\\');
$this->path = rtrim($path, '/\\') . DIRECTORY_SEPARATOR;
}
/**
* Try to load a class
*
* @param string $class The class name to load
*
* @return boolean If the loading was successful
*/
public function load($class)
{
$class = ltrim($class, '\\');
if (strpos($class, $this->namespace) === 0) {
$nsparts = explode('\\', $class);
$class = array_pop($nsparts);
$nsparts[] = '';
$path = $this->path . implode(DIRECTORY_SEPARATOR, $nsparts);
$path .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
if (file_exists($path)) {
require $path;
return true;
}
}
return false;
}
/**
* Register the autoloader to PHP
*
* @return boolean The status of the registration
*/
public function register()
{
return spl_autoload_register(array($this, 'load'));
}
/**
* Unregister the autoloader to PHP
*
* @return boolean The status of the unregistration
*/
public function unregister()
{
return spl_autoload_unregister(array($this, 'load'));
}
}
打包文件
/var/www/somedir/Twiggled/Core/Helper/Strings.php
<?php
namespace Twiggled\Core\Helper;
class Strings
{
// Note: Eventhough the method use a global PHP function name,
// there is no problem with it - thanks to namespace.
public static function str_replace($str)
{
$str = preg_replace('/\s/', '_', strtolower($str));
return preg_replace('/[^a-zA-Z_]/', '', $str);
}
}
/var/www/somedir/Twiggled/Core/Marketing/Cleanurl.php
<?php
namespace Twiggled\Core\Marketing;
use \Twiggled\Core\Helper\Strings as HelperStrings;
class Cleanurl
{
const BASE_URL = 'http://example.com/';
public $cleanPath;
public function __construct($str)
{
$this->cleanPath= HelperStrings::str_replace($str);
}
}
加载并使用它..
/var/www/somedir/index.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('date.timezone', 'Europe/Berlin');
// Bootstrap the package / register autoloader
require_once __DIR__ . '\Twiggled\bootstrap.php';
use \Twiggled\Core\Marketing as Marketing;
try {
$obj = new Marketing\Cleanurl('$This w-ork5s!');
// prints 'http://example.com/this_works'.
echo Marketing\Cleanurl::BASE_URL . $obj->cleanPath;
}
catch (\Exception $e) { }