kikko088 kikko088 - 4 years ago 183
PHP Question

php class not found but exist

I have a project with this structure:

/root
/api/class/user.php
/api/autoload.php
/api/api.php
/index.php


inside
index.php
I require (
./api/autload.php
)
inside
autoload.php
I require (
class/user.php
) and (
api.php
)

if I create a user instance inside autoload.php no problem,
but If I create a user instance inside api.php I get class not found.

where is the error?
is wrong this structure? I don't use any htaccess.

EDIT
I made some changes:
I split autoload in 2 file:

config.php -> I define some constant
autoload.php -> require config.php + require all my class
api.php -> I require autolaod.php
index.php -> I require api.php


now seems to work...the structure is correct for you?

autoload.php

define( "LIB_PATH", "lib" );
define( "CLASS_PATH", "class" );
require( LIB_PATH . "/jwt.php" );

require( CLASS_PATH . "/progetto.php" );
require( CLASS_PATH . "/azienda.php" );
require( CLASS_PATH . "/entrata.php" );
require( CLASS_PATH . "/uscita.php" );
require( CLASS_PATH . "/user.php" );

Answer Source

From what you wrote, the api.php doesn't have class/user.php required. class/user.php is required only in index.php (through autoload.php) and in api.php (directly).

The solution is to add require('./class/user.php') in the api.php file.

EDIT

Good approach would be to have all requires of class files in the autoload.php and require the autoload to index.php, api.php, and all other new files where needed.

The edit in the answer now looks like good solution.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download