嗨,我现在学习php,并且我测试了自动加载功能,但是它不起作用。我有两个文件:start.php
和myClass.php
。我在路径中./xampp/htdocs
想要的文件如果我启动start.php,请使用Autoloading来包含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'
我做错了。
需要使用spl_autoload_register
-将给定的功能注册为__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');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句