在另一个文件 PHP 中创建对象时收到'class not found'错误消息



我是WordPress和PHP的初学者,我试图通过定义用于生成页面的类,将自定义设置选项页面添加到我的WordPress主题中。当我尝试在functions.php文件中创建一个对象以生成页面时,我会收到一个错误消息,说明无法找到类。

我花了一段时间搜索解决方案并弄乱了代码,但是我找不到任何可行的东西。该文件肯定存在(我可以在文件资源管理器中的指定位置中找到它,并在我的IDE中打开/编辑)。如果我将代码从我的类文件直接粘贴到functions.php中,然后删除了类声明和构造函数,一切都按预期工作。

我正在Windows上运行XAMPP。

错误消息:

Fatal error: Uncaught Error: Class 'My_Class' not found in C:xamppmy-path-to-sitemy-themefunctions.php

in my-Site functions.php:

include('/folder/class.my-class.php');
$my_options = new My_Class;
$my_options->__construct();

in my-Site folder class.my-class.php:

class My_Class
{
    private $options;
    function __construct() {
        add_action( 'admin_menu', array($this, 'option_add_admin_menu'));
        add_action( 'admin_init', array($this, 'option_settings_init'));
    }
    function option_add_admin_menu(  ) { 
        add_options_page('My Options', 'Options', 'manage_options', 
        'options', array($this, 'option_options_page');
    }
    // rest of code that registers settings & fields
}

编辑:我更改了" include():" to" require()",但现在我收到了两个不同的错误消息:

Warning: require(/setup/class.my-class.php): failed to open stream: No such file or directory in C:xampphtdocsmy-sitewordpresswp-contentthemesmy-themefunctions.php on line 29
Fatal error: require(): Failed opening required '/setup/class.my-class.php' (include_path='C:xamppphpPEAR') in C:xampphtdocsmy-sitewordpresswp-contentthemesmy-themefunctions.php on line 29

有效地,您没有正确的路径,如果文件不存在,include将允许您继续。

包括或需要文件时,如果您提供的路径以/开头,则PHP将其视为当前文件系统根的路径。当您提供一条不以其中之一开头的路径时,PHP认为这是一条相对路径,它将尝试根据当前文件所在的位置和其他目录所知道的目录来猜测哪个文件。

要修复您可能要执行以下操作:

require_once __DIR__.'/folder/class.my-class.php';

请参阅includeinclude_once__DIR__上的文档。

建议:

每当包含文件时,您应该尽可能尝试使用require_once。如果是您知道可以多次包含的文件,则可以使用require。如果它是由于不存在的任何原因可以省略的文件,则可以使用include_once。如果文件可以兼有,则只需使用include

但是,作为一名经验丰富的程序员,我也可以告诉您,如果您使用的是include_onceinclude,则在尝试盲目包含该文件之前,应该检查是否存在文件。

另外,我强烈建议您始终保持以下代码活动。这将有助于您在有机会真正打破之前遇到破坏错误。或至少允许您更好地理解为什么会破裂。

ini_set('display_errors', '1');
error_reporting(-1);

请在代码中查看我的评论

in my-Site folder class.my-class.php:

<?php
class My_Class
{
    private $options; //if you want receive a option
    function __construct($options) { //You need receive this option here
        $this->options = $options; //and atribut it here
        //add_action( 'admin_menu', array($this, 'option_add_admin_menu'));
        //add_action( 'admin_init', array($this, 'option_settings_init'));
    }
    function option_add_admin_menu() { 
        //add_options_page('My Options', 'Options', 'manage_options', 
        //'options', array($this, 'option_options_page');
    }
    // rest of code that registers settings & fields
}

in my-Site functions.php:

    <?php
    include_once('folder/class.my-class.php'); //removed the root bar
    //You are waiting for a option in the class, so pass this option
    $my_options = new My_Class('some option'); 
    //$my_options->__construct(); //You don't need this here, the constructor is used inside the class.

相关内容

最新更新