PHP OOP:这段代码是如何工作的?
我对 PHP5/OOP 不太熟悉,无法真正理解以下代码的工作原理。在研究 __autoload() 函数时在 PHP.net 文档中找到了它。
我该怎么称呼这个? new MyClass1();
或者 autoloader::model('myModel');
?
class autoloader {
public static $loader;
public static function init()
{
if (self::$loader == NULL)
self::$loader = new self();
return self::$loader;
}
public function __construct()
{
spl_autoload_register(array($this,'model'));
spl_autoload_register(array($this,'helper'));
spl_autoload_register(array($this,'controller'));
spl_autoload_register(array($this,'library'));
}
public function library($class)
{
set_include_path(get_include_path().PATH_SEPARATOR.'/lib/');
spl_autoload_extensions('.library.php');
spl_autoload($class);
}
public function controller($class)
{
$class = preg_replace('/_controller$/ui','',$class);
set_include_path(get_include_path().PATH_SEPARATOR.'/controller/');
spl_autoload_extensions('.controller.php');
spl_autoload($class);
}
public function model($class)
{
$class = preg_replace('/_model$/ui','',$class);
set_include_path( dirname(__FILE__) . PATH_SEPARATOR.'/model/');
spl_autoload_extensions('.model.php');
spl_autoload($class);
}
public function helper($class)
{
$class = preg_replace('/_helper$/ui','',$class);
set_include_path(get_include_path().PATH_SEPARATOR.'/helper/');
spl_autoload_extensions('.helper.php');
spl_autoload($class);
}
}
I'm kind of new to PHP5/OOP and can't really get my head around how the following code works. Found it on the PHP.net documentation when researching the __autoload()-function.
How shall I call this? new MyClass1();
Or autoloader::model('myModel');
?
class autoloader {
public static $loader;
public static function init()
{
if (self::$loader == NULL)
self::$loader = new self();
return self::$loader;
}
public function __construct()
{
spl_autoload_register(array($this,'model'));
spl_autoload_register(array($this,'helper'));
spl_autoload_register(array($this,'controller'));
spl_autoload_register(array($this,'library'));
}
public function library($class)
{
set_include_path(get_include_path().PATH_SEPARATOR.'/lib/');
spl_autoload_extensions('.library.php');
spl_autoload($class);
}
public function controller($class)
{
$class = preg_replace('/_controller$/ui','',$class);
set_include_path(get_include_path().PATH_SEPARATOR.'/controller/');
spl_autoload_extensions('.controller.php');
spl_autoload($class);
}
public function model($class)
{
$class = preg_replace('/_model$/ui','',$class);
set_include_path( dirname(__FILE__) . PATH_SEPARATOR.'/model/');
spl_autoload_extensions('.model.php');
spl_autoload($class);
}
public function helper($class)
{
$class = preg_replace('/_helper$/ui','',$class);
set_include_path(get_include_path().PATH_SEPARATOR.'/helper/');
spl_autoload_extensions('.helper.php');
spl_autoload($class);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将为您提供自动加载器实例。这个类想要成为一个单例。
所有 __autoloader 都是 php 在最后一刻尝试查找尚未包含的类。如果您有一个良好的命名/目录结构,您将无需担心包含/需要您自己的任何类...自动加载器将为您做这件事。
如果你想调用模型函数:
这将在调用上面的静态 init() 函数之后完成。
That will give you the autoloader instance. This class wants to be a singleton.
All __autoloader is is a last minute attempt by php to find a class that hasn't been included yet. If you have a good naming/directory structure you won't need to worry about including/requiring any of your own classes...the autoloader will do it for you.
if you wanted to call the model function:
This would be done after calling the static init() function from above.