0
votes

Problem: I have an index.php file which has several composer dependencies. Inside the index.php file i'm trying to call the static method from the outside class in a different php (let's say auth.php) file like this:

/*creating a class instance*/
$var = new AuthClass();

/*accessing an outside class method*/
$var = AuthClass::checkTime($tokenCode);

The issue is the checkTime method inside the class requires a composer dependency as well, which isn't inherited, although the file is located in the same folder as index.php and index.php is included.

PHP Fatal error:  Uncaught Error: Class 'Token' not found

I've tried everything - from adding require_once/include 'index.php' to copying the composer autoload to auth.php outside and inside the AuthClass code, but nothing works, i'm still getting the same error.

Additional code:

index.php

require __DIR__ . '/src/vendor/autoload.php';

$argument1 = $_GET['argument1'];
$tokenCode = $_GET['tokenCode'];

include 'config/database.php';
include 'objects/program1.php';
include 'auth.php';

use ReallySimpleJWT\Token;
use Carbon\Carbon;

$secret = "somesecret";

if (($_SERVER['REQUEST_METHOD']) == "GET") {

    if ($_GET['url'] == "bankquery") {

        if($tokenCode===NULL){
            echo "no correct token provided";
            print($results);
        } else {
        $results = Token::validate($tokenCode, $secret);
        if ($results = 1){

$var = new AuthClass();
$var = AuthClass::checkTime($tokenCode);

} else {
    echo "no correct token provided";
}
    }

} else {
    echo "some GET other query";
}

?>

auth.php

// loading composer
require __DIR__ . '/src/vendor/autoload.php';

//loading my index.php file
include 'index.php';

//using composer dependencies
use ReallySimpleJWT\Token;
use Carbon\Carbon;

class AuthClass{

public static function checkTime($tokenCode){

// getting payload from token code by accessing the composer dependency method in a class Token
$received = Token::getPayload($tokenCode);

return $received;
}
}

?>

Need help, guys.

2
There is no enough information. Please provide some info about Token class - Davit Huroyan
@DavitHuroyan provided the code - David Robertson
try \ before Token " \Token" or try with namespace lets see what will return - Davit Huroyan
And what is the content of index.php? - rob006
tried both the "\Token" and namespace "ReallySimpleJWT\Token" none worked, same error, unfortunately @DavitHuroyan - David Robertson

2 Answers

1
votes

You're using AuthClass before it was defined - try move include 'index.php'; line at the end of the file.

You should also include vendor/autoload.php only once - you don't need to repeat this in every file, just make sure that it is included at the top of the entry file which handles request.

But this is more like a result of design problem. You should define AuthClass in separate file and avoid any additional side effects in it - file should only define class. This is part of PSR-1 rules:

Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.

Since you're already using autoloader from Composer it should be relatively easy to register your own autoloading rules, so Composer's autoloader will take care about classes autoloading.

If at this point you're still getting Class 'X' not found, you probably did not installed some dependency or your autoloading rules are incorrect.

1
votes

The simplest solution would be to include your own code in the composer autoloading. The composer website tells you how to do it.

You don't need to require the composer files yourself and composer handles everything for you.

The PSR-4 tells you how to namespace your code to use Namespacing.