Fatal error: Class 'database' not found - PHP
When I attempt 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 problem.php I require database.php. What is happening?
problem.php
<?php
require("common.ph开发者_JS百科p");
require("database.php");
...
?>
database.php
<?php
class database
{
...
}
?>
this is probably an include path issue. In order to fix it, in your problem.php file
do this:
echo realpath (dirname(__FILE__));
that will output something like
/var/www/html/myfilepath/
your file, problem.php will be in that dir.
now, if database.php is also in that dir, you can do this
$filepath = realpath (dirname(__FILE__));
require_once($filepath."/database.php");
if it is somewhere else you can do
require_once($filepath."/../../path/to/somewhere/else/database.php");
do you include the file?
include "database.php"; // or the path relative to database.php
class problem
{
nevermind. Maybe :the include (required) is not opening the file.
Can you add
echo "OK";
to footer of the database.php and check again?
So, we can understand, database.php is really included to page .
for posterity: just to clarify the reason this fails is that you are including from the point of view of the ORIGINAL call let me explain giving the example:
myfolder/index.php
<?php
include ("classes/problem.php");
...
?>
this means that by the time you get to "problem.php
" and mention "database.php
" you're in "myfolder/
" as far as PHPland is concerned not "myfolder/classes
" (which you'd need to be in for this to make sense). The right way to do it is to simply use absolute paths for everything via a constant made on line 1 page 1. I know it's what a lot of people say is horrible - but in all honesty it's what "namespaces" are supposed to fix but I would still use constants (IMHO namespaces are awful in php, and are more to do with self-aggrandisement/ open source advertising/ authorship ego boasting agenda that anything concrete or useful)
精彩评论