سؤال

I have a problem with classes which cannot be found in PHP.

The first thing I do is 'require_once' a file which 'require_once's all other files. When loading, no problems are showed. But when I start calling my function (Users::verificate();) I get the following error:

Fatal error: Class 'Users' not found in /Applications/XAMPP/xamppfiles/htdocs/sparks/dashboard.php on line 4

To test I've added a simple class with a function which only outputs a string with the echo method. This works so the problem has to be with this class. A MySql function which I call like this just works.

$mySql = new MySql();
$mySql->executeQuery('...');

The simple class has static a static function which I call like this (Oh, this works):

simple::launch();

In the Users class I'm calling non static functions from the MySql class from a static function. Can this be the problem?

Like another question here on SO suggested the problem isn't in using short php opening tags instead of the traditional php opening tag.

Even a little hint may help me :). Thanks for your time!

Edit: I've added some relevant code from the User class. This is basically what it all looks like:

<?php
class Users {
    public static function authenticate($email, $password) {
        $mySql = new MySql();

        $mySqlResult = $mySql->executeQuery("selectUser", [$email]);

        ...
    }

    public static function isAdmin() {
        if ($_SESSION['isAdmin']) {
            return true;
        }
        return false;
    }
    ...
}

Edit 2: I'm trying to show the flow: From dashboard.php this are the first code lines:

<?php
    require_once('code/init.php');
    simple::launch();
    if (!Users::verify()) {
        header("Location: index.php");
    }
?>

simple::launch(); is the code I used to test. This executes well. From this on the init.php file looks like this:

<?php
session_start();

require_once('simple.php');
require_once('MySql.php');
require_once('Users.php');
require_once('Projects.php');

The file names are correct as I get a visible error when these are wrong.

dashboard.php exists in the root. From there is a folder called 'code' which contains all these files.

هل كانت مفيدة؟

المحلول

@MbRostami gave the advice in a comment to use the 'get_required_files()' function to see which files are included. It turns out that the wrong files were loaded.

Root
| dashboard.php
| users.php
|- code
 | Users.php
 | init.php

In the init.php file the Users.php file (both files are in the code folder) was required. But for some reason the users.php file from the root was loaded. Some strange behaviour imho. Ahwell, that's something to investigate during the christmas days.

Problem is solved! Thanks!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top