KoenDG
KoenDG

Reputation: 105

PHP use statement failing

I wanted to try out the FlawkApi library and copied the sample page for Steam accounts.

PHP is generating an error however:

Fatal error: Class 'FlawkApi\Services\Steam' not found in C:\xampp\htdocs\SteamAccount\index.php on line 18

The code in question is this:

use FlawkApi\Services\Steam;

// include bootstrap
require_once (__DIR__.'/bootstrap.php');

// initiate Steam with example gamerid and httpClient
$FlawkApi = new Steam($gamerIds['steam']['id'], $httpClientProvider());

That last line is line 18.

Bootstrap.php holds this:

// error reporting on
error_reporting(E_ALL);
ini_set('display_errors', 1);

// include some example gamer ids
require_once (__DIR__.'/init.php');

And init.php holds this(modified ID here, real ID is in the actual code):

$gamerIds = array(
    'steam' =>
    array(
        'id' => 'myId'
    )
);

$httpClientProvider = function() {
            return new \FlawkApi\Common\Http\Client\FlawkClient();
        };

This is a simple use statement that PHP is falling over. My PHP version is 5.4.7, installed from the latest XAMPP version.

Does anyone know what this wouldn't work?

EDIT: initialized the composer.json, it created the vendor folder. I then required vendor/autoload.php, but it still generates the same error.

EDIT2: Here's a screenshot of the folder structure and the composer.json file: enter image description here

Upvotes: 1

Views: 601

Answers (1)

vishal
vishal

Reputation: 4083

I think you can use spl_autoload_register to register function which can load the class if class is not found.

e.g

function my_autoloader($class) {
   if(file_exists(__DIR__ . $class . ".php"))
        require_once(__DIR__ . $class . ".php");
}

spl_autoload_register('my_autoloader');

Upvotes: 1

Related Questions