【问题标题】:My autoload function doesnt work but it seems the class is loaded PHP我的自动加载功能不起作用,但似乎该类已加载 PHP
【发布时间】:2018-03-05 20:11:02
【问题描述】:

我是一名新的 PHP 开发人员,我在使用自动加载功能时遇到了麻烦。这是错误。

public class dog{ public function hey($var){ echo $var; } }
Fatal error: Uncaught Error: Class 'dog' not found in C:\xampp\htdocs\ebay\index.php:3 Stack trace: #0 {main} thrown in C:\xampp\htdocs\ebay\index.php on line 3

它显示了类,但随后回显了一个错误。

这是自动加载功能。

require_once("config.php");
spl_autoload_register("my_auto_load");

function my_auto_load($class_name){
    $path = "classes";
    require_once($path.DS.$class_name.".php");
}

这是索引文件

require_once("config.php");
spl_autoload_register("my_auto_load");

function my_auto_load($class_name){
    $path = "classes";
    require_once($path.DS.$class_name.".php");
}

这是索引文件

<?php 
require_once("inc/autoload.php");
$dog= new dog();
$dog->hey("KLK");
?>

【问题讨论】:

    标签: php html css web autoload


    【解决方案1】:

    您可以采取一些措施来追踪您的问题。尝试更新您的自动加载功能,使其如下所示:

    function my_auto_load($class_name){
      $path = "classes";
    
      $includeFilename = $path.DS.$class_name.".php";
    
      ?>
      Class: <?= $class_name; ?><br>
      Include Filename: <?= $includeFilename; ?><br>
      Include Filename Real Path: <?= realpath($includeFilename); ?><br>
      Current working directory: <?= getcwd(); ?><br>
      Does the include file exists? <?= file_exists($includeFilename) ? "Yes, it exists" : "No, it doesn't exist"; ?><br>
      <?php
    
      require_once($includeFilename);
    }
    

    这将显示您的自动加载功能是否正在运行,如果它正在运行,它实际上试图包含什么文件以及该文件是否存在。如果它正确创建了文件路径并且文件存在,那么您的问题可能出在其他地方。

    我重新创建了您的项目,它似乎工作正常。这是我的目录结构:

    ./classes/dog.php
    ./inc/autoload.php
    ./index.php
    

    index.php

    <?php
    
    require_once("inc/autoload.php");
    $dog= new dog();
    $dog->hey("KLK");
    

    inc/autoload.php

    <?php
    
    spl_autoload_register("my_auto_load");
    function my_auto_load($class_name){
        $path = "classes";
    
        $includeFilename = $path.'/'.$class_name.".php";
    
    ?>
      Class: <?= $class_name; ?><br>
      Include Filename: <?= $includeFilename; ?><br>
      Include Filename Real Path: <?= realpath($includeFilename); ?><br>
      Current working directory: <?= getcwd(); ?><br>
      Does the include file exists? <?= file_exists($includeFilename) ? "Yes, it exists" : "No, it doesn't exist"; ?><br>
    <?php
    
        require_once($includeFilename);
    }
    

    classes/dog.php

    <?php
    
    class dog {
        public function hey($msg) {
            echo $msg;
        }
    }
    

    【讨论】:

    • 现在它不再打印类了,只是给我这个错误致命错误:未捕获的错误:在 C:\xampp\htdocs\ebay\index.php:3 中找不到类“狗”堆栈跟踪: #0 {main} 在第 3 行的 C:\xampp\htdocs\ebay\index.php 中抛出
    • 您可以尝试在 require_once 之前添加类似if($class_name == 'dog') die(); 的内容。然后您至少会看到打印的摘要。
    • 尝试将var_dump($class_name); die(); 放在 my_auto_load 函数的开头。
    • 尝试将DS 常量替换为字符串'/'。例如。 $includeFilename = $path.'/'.$class_name.".php";
    • 这就是我声明常量的方式 defined("DS") ||定义("DS",DIRECTORY_SEPARATOR);
    猜你喜欢
    • 1970-01-01
    • 2020-04-14
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 2011-06-15
    • 1970-01-01
    • 2016-12-26
    相关资源
    最近更新 更多