Php __自动加载($class)不工作?找不到类错误
下面是Php __自动加载($class)不工作?找不到类错误,php,class,object,autoload,Php,Class,Object,Autoload,下面是PRO-PHP和JQUERY一书中的一些示例,但由于某些原因,这些示例不起作用。即使是我从图书网站下载的例子也不起作用。我不确定出了什么问题,因为我做的和书上写的一模一样 /public/Index.php include_once '../sys/core/init.inc.php'; $cal = new Calendar($dbo, "2010-01-01 12:00:00"); //ERROR Class 'Calendar' not found /sys/core/init.i
PRO-PHP和JQUERY
一书中的一些示例,但由于某些原因,这些示例不起作用。即使是我从图书网站下载的例子也不起作用。我不确定出了什么问题,因为我做的和书上写的一模一样
/public/Index.php
include_once '../sys/core/init.inc.php';
$cal = new Calendar($dbo, "2010-01-01 12:00:00"); //ERROR Class 'Calendar' not found
/sys/core/init.inc.php
function __autoload($class)
{
$filename = "../sys/class/class." . $class . ".inc.php";
if ( file_exists($filename) )
{
include_once $filename;
}
}
/sys/class/class.calendar.inc.php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
/**
* Create a database containg relevant info
*
* @param object $dbo a database object
* @param string $useDate the date to build calender
*/
public function __construct($dbo=NULL, $useDate=NULL)
{
/*
* Call the parent constructor to check db object
*/
parent::__construct($dbo);
}
}
这是非常恼人的,因为书中的每一章都是建立在这个简单的基础之上的。我想问题出在
\uu autoload()
,但我不知道 文件路径没有指向正确的点
更好的方法是在Index.php
中这样做
define('DOCROOT', dirname(__FILE__));
…然后像这样修改您的\uuu autoload()
function __autoload($class)
{
$filename = DOCROOT . "/sys/class/class." . strtolower($class) . ".inc.php";
if ( file_exists($filename) )
{
include_once $filename;
}
}
在包含文件名之前,你应该
strotlower()
文件名,因为你的类是Calendar
,但是你的文件名有Calendar
,首先,根据官方文件,不鼓励你在新代码中使用u autoload(),因为它将来可能会被弃用或删除;相反,请使用spl_autoload_register(),如下所示:
请注意我们是如何将$className设置为strtolower()的。这是为了确保类名(通常以大写字母开头)与文件名(通常都以小写字母开头)一致。在某些环境中(尤其是Windows),这可能不是必需的,但只是为了更安全。例如,我不必在我的开发环境Windows中使用它,但我的生产环境(debian)不会使用它
然后将该函数名作为参数传递给spl_autoload_regsiter。请注意,函数名是string
//Now use spl_autoload_register()
spl_autoload_register( 'myAutoLoader' );
如果希望捕获异常,可以在自定义函数中执行如下操作:
//first define a custom function with exception handling
function myAutoLoader( $className ){
$path = strtolower( path/to/your/class/ . $className . '.php' );
include_once( $path );
if( !class_exists( $className, false ) ){
throw new RuntimeException( 'Class '. $className . ' has not been
loaded yet' );
}
}
//then the spl_autoload_register(), just like before
spl_autoload_register( 'myAutoLoader' );
然后,在声明类时,您必须捕获抛出的异常。我通过使用
获得此类型错误。
替换了“
,如果使用”
,则不存在此错误:
正确的文字:
function __autoload($class) {
echo HOME_INC."/$class.class.php";
if(is_file(HOME_INC."/$class.class.php")){
include_once HOME_INC."/$class.class.php";
}elseif(is_file(ADMIN_INC."/$class.class.php")){
include_once ADMIN_INC."/$class.class.php";
}
}
错误的文字:
function __autoload($class) {
echo HOME_INC.'/$class.class.php';
if(is_file(HOME_INC.'/$class.class.php')){
include_once HOME_INC.'/$class.class.php';
}elseif(is_file(ADMIN_INC.'/$class.class.php')){
include_once ADMIN_INC.'/$class.class.php';
}
}
Windows上的文件名不区分大小写,大多数UNIX版本都区分大小写。也许代码是针对Windows的。@AbiusX为什么不必要地将代码绑定到平台上,尤其是PHP?我不会,但教程的作者可能会这么做!我从不使用Windows。AlEX是正确的,但是我也会考虑使用多个AutoLoad函数,如果不是由外部库存在的话,没有提到官方文档中的贬义:“贝诺,你确定吗?”请允许我引用您引用的同一链接:“警告:从PHP7.2.0开始,此功能已被弃用。强烈建议不要依赖此功能。”您是对的,这可能是最近更改的。谢谢你指出这一点。
function __autoload($class) {
echo HOME_INC.'/$class.class.php';
if(is_file(HOME_INC.'/$class.class.php')){
include_once HOME_INC.'/$class.class.php';
}elseif(is_file(ADMIN_INC.'/$class.class.php')){
include_once ADMIN_INC.'/$class.class.php';
}
}