Fatal error: "Database" not found - PHP

When I try to use the problem class, I get the following error:

Fatal error: Class 'database' not found in path/problem.php on line 25 

I don't understand why I got this error, at the top of the .php problem I need database.php. What's happening?

problem.php

 <?php require("common.php"); require("database.php"); ... ?> 

database.php

 <?php class database { ... } ?> 
+6
php
source share
4 answers

this is probably a path inclusion problem. To fix this, in the file problem.php

do the following:

 echo realpath (dirname(__FILE__)); 

which will output something like

/ var / WWW / HTML / MyFilePath /

your file, the .php problem will be in this directory.

now if database.php is also in this directory, you can do it

 $filepath = realpath (dirname(__FILE__)); require_once($filepath."/database.php"); 

if it's somewhere else you can do

 require_once($filepath."/../../path/to/somewhere/else/database.php"); 
+11
source share

Do you include the file?

  include "database.php"; // or the path relative to database.php class problem { 

nevermind. Maybe: include (required) does not open the file.

+1
source share

Can you add

 echo "OK"; 

to the .php database footer and check again?

So we can understand that database.php is really included in the page.

0
source share

for posterity: just to clarify the reason for this fails, you turn on in terms of the ORIGINAL call, let me explain an example:

 myfolder/index.php <?php include ("classes/problem.php"); ... ?> 

this means that by the time you get to " problem.php " and specify " database.php ", you are in " myfolder/ ", since phpland does not refer to " myfolder/classes " (that you need to be in that, to make sense). The right way to do this is to simply use the absolute paths for everything, using the constant made in line 1 on page 1. I know that many say that this is terrible - but, frankly, this is what the “namespaces” should fix, but I would still use constants (IMHO namespaces are terrible in php and more related to self-aggrandizement / open source / ego authorship praising the agenda, something concrete or useful)

-one
source share

All Articles