David Robertson
David Robertson

Reputation: 1581

Calling a method from outside class with dependencies PHP

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.

Upvotes: 0

Views: 339

Answers (2)

rob006
rob006

Reputation: 22174

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.

Upvotes: 1

j_elfering
j_elfering

Reputation: 3190

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.

Upvotes: 1

Related Questions