致命错误:找不到“数据库”类 - PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4716783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal error: Class 'database' not found - PHP
提问by SamB
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 的顶部我需要database.php。怎么了?
problem.php
问题.php
<?php
require("common.php");
require("database.php");
...
?>
database.php
数据库.php
<?php
class database
{
...
}
?>
回答by Zak
this is probably an include path issue. In order to fix it, in your problem.php file
这可能是一个包含路径问题。为了修复它,在您的 problem.php 文件中
do this:
做这个:
echo realpath (dirname(__FILE__));
that will output something like
这将输出类似
/var/www/html/myfilepath/
/var/www/html/myfilepath/
your file, problem.php will be in that dir.
您的文件 question.php 将在该目录中。
now, if database.php is also in that dir, you can do this
现在,如果 database.php 也在那个目录中,你可以这样做
$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");
回答by magallanes
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.
没关系。也许:包含(必需)没有打开文件。
回答by Eray
Can you add
你能加
echo "OK";
to footer of the database.php and check again?
到database.php的页脚并再次检查?
So, we can understand, database.php is really included to page .
因此,我们可以理解,database.php 确实包含在 page 中。
回答by conners
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)
这意味着当你到达“ problem.php
”并提到“ database.php
”时,你在“ myfolder/
”中,就PHPland而言,不是“ myfolder/classes
”(你需要在其中才能有意义)。正确的做法是通过第 1 页第 1 行上的常量简单地为所有内容使用绝对路径。我知道很多人说这是可怕的 - 但老实说,这就是“命名空间”应该修复的我仍然会使用常量(恕我直言,命名空间在 php 中很糟糕,而且更多地与自我夸大/开源广告/作者自我吹嘘议程有关,任何具体或有用的东西)