如何在PHP中使用自动加载?
嗨,我现在学习php,我测试了自动加载,但它不工作。我有两个文件:如何在PHP中使用自动加载?,php,file,autoload,htdocs,Php,File,Autoload,Htdocs,嗨,我现在学习php,我测试了自动加载,但它不工作。我有两个文件:start.php和myClass.php。路径/xampp/htdocs中的文件以及我想要的如果我启动start.php,那么使用自动加载的文件包括myClass.php和此函数 这是我的代码: start.php <?php function _autoload($classname){ $filename = "./".$classname.".php"; include_onc
start.php
和myClass.php
。路径/xampp/htdocs
中的文件以及我想要的如果我启动start.php,那么使用自动加载的文件包括myClass.php
和此函数
这是我的代码:
start.php
<?php
function _autoload($classname){
$filename = "./".$classname.".php";
include_once($filename);
}
$obj = new myClass();
?>
myClass.php
<?php
class myClass {
public function _construct(){
echo "Die Klasse wurde erfolgreich erzeugt";
}
}
?>
我得到这个错误:
致命错误:在第7行的D:\Webserver\xampp\htdocs\start.php中找不到类“myClass”
我做错了什么 它是\u autoload()
,而不是\u autoload()
。前面有两个下划线
这同样适用于\u construct()
函数
注意:PHP手册建议使用spl\u autoload\u register()
而不是\u autoload()
函数,因为它允许更大的灵活性。另外,\uuuuuAutoload()
函数预计将来会被弃用。它是\uuuAutoload()
,而不是\uAutoload()
。前面有两个下划线
这同样适用于\u construct()
函数
注意:PHP手册建议使用spl\u autoload\u register()
而不是\u autoload()
函数,因为它允许更大的灵活性。另外,\uuuuuAutoload()
函数预计将来会被弃用。它是\uuuAutoload()
,而不是\uAutoload()
。前面有两个下划线
这同样适用于\u construct()
函数
注意:PHP手册建议使用spl\u autoload\u register()
而不是\u autoload()
函数,因为它允许更大的灵活性。另外,\uuuuuAutoload()
函数预计将来会被弃用。它是\uuuAutoload()
,而不是\uAutoload()
。前面有两个下划线
这同样适用于\u construct()
函数
注意:PHP手册建议使用
spl\u autoload\u register()
而不是\u autoload()
函数,因为它允许更大的灵活性。另外,\uuu autoload()
函数预计将来会被弃用。需要使用spl\u autoload\u寄存器
-将给定函数注册为\uu autoload()
实现
function _autoload($class) {
$filename = $classname.".php"; //assumed, your class file and other files are in same directory
include_once($filename);
}
spl_autoload_register('_autoload');
需要使用
spl\u autoload\u寄存器
-将给定函数注册为\u autoload()
实现
function _autoload($class) {
$filename = $classname.".php"; //assumed, your class file and other files are in same directory
include_once($filename);
}
spl_autoload_register('_autoload');
需要使用
spl\u autoload\u寄存器
-将给定函数注册为\u autoload()
实现
function _autoload($class) {
$filename = $classname.".php"; //assumed, your class file and other files are in same directory
include_once($filename);
}
spl_autoload_register('_autoload');
需要使用
spl\u autoload\u寄存器
-将给定函数注册为\u autoload()
实现
function _autoload($class) {
$filename = $classname.".php"; //assumed, your class file and other files are in same directory
include_once($filename);
}
spl_autoload_register('_autoload');
好朋友+很好的寻找伴侣+很好的寻找伴侣+很好的寻找伴侣+1不要创建自己的。使用已创建的。不要创建自己的。使用已创建的。不要创建自己的。使用已创建的。不要创建自己的。使用创建的一个。
\u-construct()
应\u-construct()
确保自动加载函数中的类名路径正确\u-construct()
应\u-construct()
应\u-construct())
确保自动加载函数中的类名路径正确\u construct()
应\u construct()
确保自动加载函数中的类名路径正确