tpsReports
tpsReports

Reputation: 47

Receiving 'class not found' error message when creating object in another file PHP

I'm a beginner to WordPress and PHP, and I'm trying to add a custom settings options page to my WordPress theme by defining a class that is used to generate the page. When I attempt to create an object in the functions.php file to generate the page, I get an error message stating that the class cannot be found.

I've spent a while searching for solutions and messing with the code, but I couldn't find anything that works. The file definitely exists (I can find it in the specified location in file explorer and open/edit it in my IDE). If I just paste the code from my class file directly into functions.php with the class declaration and constructor removed, everything works as expected.

I'm running XAMPP on Windows.

Error message:

Fatal error: Uncaught Error: Class 'My_Class' not found in C:\xampp\my-path-to-site\my-theme\functions.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
}

EDIT: I changed "include():" to "require()" as suggested, but now I am getting two different error messages:

Warning: require(/setup/class.my-class.php): failed to open stream: No such file or directory in C:\xampp\htdocs\my-site\wordpress\wp-content\themes\my-theme\functions.php on line 29

Fatal error: require(): Failed opening required '/setup/class.my-class.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\my-site\wordpress\wp-content\themes\my-theme\functions.php on line 29

Upvotes: 0

Views: 1310

Answers (2)

Fellipe Sanches
Fellipe Sanches

Reputation: 8135

Please check my comments inside the code

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.

Upvotes: 0

Nicholas Summers
Nicholas Summers

Reputation: 4756

Effectively, you don't have the right path and include will allow you to continue if the file doesn't exist.

When including or requiring a file, if the path you supply starts with a / or \ then PHP will treat it as a path from the root of the current filesystem. When you supply a path that doesn't start with one of those, PHP thinks it is a relative path it will try to guess which file to include based on where the current file is and other directories it knows about.

To fix you will likely want to do the following:

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

See the docs on include, include_once, and as well as __DIR__.

Recommendation:

Whenever including a file you should try to use require_once whenever possible. If it is a file that you know can be included multiple times then you may use require. If it is a file that is OK to be omitted if it for whatever reason doesn't exist, then you may use include_once. If the file can be both, only then should you use include.

However, as an experienced programmer I can also tell you that if you are using either include_once or include you are doing something wrong and should be checking if a files exists before trying to blindly include it.

Also, I highly recommend having the below code active at all times. This will help you catch breaking errors before they have a chance to actually break. Or at least grant you a better understanding of why something broke.

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

Upvotes: 1

Related Questions