Loading Classes without Composer (Autoloader) - Not found

172
August 15, 2018, at 9:30 PM

I'm not able to use composer so I have to do it in a different way, initially I used require_once to load every individual class but that didn't work out either so now I'm using an autoloader instead but I still face the same or similar problems:

The page where it starts:

<?php
declare(strict_types=1);
require __DIR__ . '/ERC20/autoload.php';
use ERC20\ERC20;
use ERC20\EthereumRPC;
$geth = new EthereumRPC('127.0.0.1', 8545);

This is the autoload file:

<?php
spl_autoload_register(function($class){
    $path = __DIR__ . '/' . $class . '.php';
    if(file_exists($path))
        require $path;
});
?>

The classes are stored in a folder named ERC20 that's in the same directory as the Client account php file.

This is the error it throws:

Fatal error: Uncaught Error: Class 'ERC20\EthereumRPC' not found in /home/sewicumg/public_html/contenthourlies.com/wp-content/themes/seoexp/account_client.php:35

Line 35 is this line:

$geth = new EthereumRPC('127.0.0.1', 8545);

The file EthereumRPC.php is present in the folder ERC20, and I'm using PHP7.1

Someone suggested to use an autoloader so that's what I did but to no avail thus far.

Answer 1

I fixed it by creating a new folder named Vendor, moved the ERC20 folder in that folder and moved the autoload.php there as well.

I also used a different autoload that I found on Youtube, and now all the classes get loaded without error.

This is the the autoload code:

<?php
spl_autoload_register(function($class){
    $prefix = 'ERC20\\';
    $length = strlen($prefix);
    $path = __DIR__ . '/ERC20/';
    if (strncmp($prefix, $class, $length) !==0) {
        return;
    }
    $relative_class = substr($class, $length);
    $file = $path . str_replace ('\\', '/', $relative_class) . '.php';
    if(file_exists($file)) {
        require $file;
    }
});
?>

And this is how I call the autoload in the page:

require 'vendor/autoload.php';
Rent Charter Buses Company
READ ALSO
How to handle data when using classes - PHP [on hold]

How to handle data when using classes - PHP [on hold]

I am currently working within CodeIgniter and I am struggling to understand how to properly use Anemic or Rich Entity Classes within my programming

163
Getting a blanck page after creating an image [duplicate]

Getting a blanck page after creating an image [duplicate]

This question already has an answer here:

136
how to select 2 sql tables together?

how to select 2 sql tables together?

I havew for example 'employees' table and 'phones' tables , now i select them together

118