Class 未发现错误,使用autoload 来使用class

Class not found error, using autoload to use class

出现错误:

Class 'location' not found in C:\wamp\www\New folder\index.php on line....

项目文件位置:http://localhost/New%20folder/ 这里 class 文件和索引文件都被保存了。

Class 文件:location.class.php

class location
{
    function add_location()
    {
        echo 'Its working!';
    }
}

我的代码(index.php根目录下的文件)

<?php
function __autoload($class_name) {
      if (file_exists($class_name . '.class.php')) 
      {
          require_once ($class_name . '.class.php');
      }   
}
try {
    $location = new location();
} catch (Exception $e) {
    echo $e->getMessage(), "\n";
}
?>
<html>
    <head>
        <title>Autoload Testing</title>
    </head>    
    <body>
    </body>
</html>

您错过了 location.class.php 中的起始 php 标记。

<?php

class location
{
    function add_location()
    {
        echo 'Its working!';
    }
}

(结束标记 ?> 在文件末尾是可选的。)

更改您的 location.class.php 内容如下..

  



//php opening
function __autoload($class_name) {



      if (file_exists( dirname( __FILE__ ).'/'.$class_name . '.class.php')) 
      {
          require_once (dirname( __FILE__ ).'/'.$class_name . '.class.php');
      }   



}    
//php closing