找不到 PHP 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1179638/
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
PHP class not found
提问by dnagirl
I solved this question my own. The filename was wrong lolz.
我自己解决了这个问题。文件名是错误的lolz。
Hello everyone!
大家好!
I'm building a CMS like Drupal and Joomla. I'm working on the module feature (plugins), and I got the following error:
我正在构建一个像 Drupal 和 Joomla 这样的 CMS。我正在研究模块功能(插件),但出现以下错误:
Fatal error: Class 'settings' not found in C:\wamp\www\SYSTEM\view.php on line 22
Here is my code:
这是我的代码:
start.php
启动文件
<?php
//First of all, start with some advertisement
header("X-Powered-By:ZOMFG CMS, and ofcourse PHP, but that's less important");
//Then less impotant stuff lololol.
session_start(); //Start a session
mysql_connect($db_host, $db_user, $db_pass); //Connect to database
mysql_select_db($db_name); //Select a database
//Load core
require_once("core.php");
//Load modules
$res_modules = mysql_query("SELECT * FROM ".$_SERVER["db_prefix"]."modules WHERE enabled=1");
echo mysql_error();
$module_exists = array();
while($row_modules = mysql_fetch_array($res_modules))
{
//Load module
$name = $row_modules["name"];
modules::load_module($name);
//and initialize it
eval($name."::init();");
//Yes, it exists
$module_exists[$name] = true;
}
//Check if the user wants shit from a module
if(isset($_GET["m"]))//Yes the user want it
{
//Does the module exist and activated, and has it a function called view?
if(isset($module_exists[$_GET["m"]]) && method_exists($_GET["m"], "view"))//Yep
{
//Load view (should be an array)
eval("$module_view = ".$_GET["m"]."::view();");
if(!is_array($module_view))//Not an array :(
{
error::e500module($_GET["m"], $_SERVER["REQUEST_URI"]);
}
else//The error would kill the entire script, m'kay
{
view::index();
}
}
else//Nope, so display error
{
error::e404($_SERVER['REQUEST_URI']);
}
}
settings.php
设置.php
<?php
class settings
{
function get($what)
{
$result_get = mysql_query("SELECT value FROM ".$_SERVER["db_prefix"]."settings WHERE key='$what'");
if(mysql_num_rows($result_get) > 0)
{
$row_get = mysql_fetch_array($result_get);
return $result_get["value"];
}
else
{
return -1;
}
}
}
core.php
核心文件
<?php
//Load core classes
require_once("settings.php");
require_once("error.php");
require_once("theme.php");
require_once("view.php");
require_once("modules.php");
view.php
查看.php
<?php
class view
{
function head()
{
include("../THEMES/".settings::get("theme")."/head.php");
}
function foot()
{
include("../THEMES/".settings::get("theme")."/foot.php");
}
function left()
{
include("../THEMES/".settings::get("theme")."/left.php");
}
function right()
{
include("../THEMES/".settings::get("theme")."/right.php");
}
function index()
{
include("../THEMES/".settings::get("theme")."/index.php");
}
}
Start.php is obviously executed first. Not other pages are executed before it, except customsettings.php that contains database information. If I used $_SERVER["db_prefix"] in my code above, it's because I needed a superglobal which is set in customsettings.php:
Start.php 显然是最先执行的。除了包含数据库信息的 customsettings.php 之外,不会在它之前执行其他页面。如果我在上面的代码中使用了 $_SERVER["db_prefix"],那是因为我需要一个在 customsettings.php 中设置的超全局变量:
customsettings.php
自定义设置.php
<?php
$db_host = "localhost"; //Database host
$db_user = "root"; //Database user
$db_pass = "you may not know this"; //Database password
$db_name = "zomfg"; //Database name
$_SERVER["db_prefix"] = "zomfg_";//Prefix, needs to be superglobal
Can anybody help me? It seems that view.php's index function is called before settings.php is included. Sorry if this question is huge, I just want to be clear. Also don't say eval() is evil, I know.
有谁能够帮助我?似乎在包含 settings.php 之前调用了 view.php 的 index 函数。对不起,如果这个问题很大,我只想说清楚。也不要说 eval() 是邪恶的,我知道。
So I want to know why the settings class could not be found. If you need more source code, please comment to this question.
所以我想知道为什么找不到设置类。如果您需要更多源代码,请对此问题发表评论。
回答by dnagirl
Though you would expect settings.php to be available to view.php because it was included in a script that includes them both, I have found that this usually isn't the case. You have a couple of choices:
尽管您希望 settings.php 可用于 view.php,因为它包含在包含它们的脚本中,但我发现通常情况并非如此。你有几个选择:
require_onceall the files each class needs in each class file- write an
__autoload()function so that PHP can find all your classes whenever it thinks it needs one
require_once每个类在每个类文件中需要的所有文件- 编写一个
__autoload()函数,以便 PHP 可以在它认为需要时找到所有类
The 2nd option is more flexible.
第二种选择更灵活。
If you want to know classes are available from a particular place try outputting get_declared_classes()
如果您想知道某个特定地方的课程是否可用,请尝试输出 get_declared_classes()
回答by Konstantin
The following does not appy in OP's case but might help others.
以下内容不适用于 OP 的情况,但可能对其他人有所帮助。
Check whether your code uses short tags <?instead of <?phpand if yes, then check your php.ini for short_open_tagsetting.
检查您的代码是否使用短标签<?代替,<?php如果是,则检查您的 php.ini 进行short_open_tag设置。
By default it is off but if you inherit your php installation from someone else...
默认情况下它是关闭的,但如果你从其他人那里继承了你的 php 安装......
回答by ohnoes_stackoverflown
Just in case somebody stumbles onto this question, I had this problem too and I solved it by making sure that the name of the php file was the same as the name of the php class inside the actual file.
以防万一有人偶然发现这个问题,我也遇到了这个问题,我通过确保 php 文件的名称与实际文件中的 php 类的名称相同来解决它。
Silly, I know.
傻,我知道。
回答by JG Estiot
There is another problem that may occur and it is worth for anyone to know it. If you use __autoload() and in the file that holds the class being autoloaded you write your PHP tags incorrectly, it will return a class not found error:
还有一个可能发生的问题,值得任何人知道。如果您使用 __autoload() 并且在包含正在自动加载的类的文件中错误地编写了 PHP 标记,它将返回一个找不到类的错误:
File App.php
文件 App.php
<?
class App extends something
{
function __construct()
{
}
}
?>
file index.php
文件 index.php
<?php
function __autoload($classname) {
$filename = "./classes/". $classname .".php";
print("Loading $filename<br>\n");
include_once($filename);
}
$app = new App();
?>
The above code does not work. For it to work you need to replace the short opening PHP tag App.php with a long one:
上面的代码不起作用。为了让它工作,你需要用一个长的替换短的开始 PHP 标签 App.php :
<?php
class App extends something
{
function __construct()
{
}
}
?>
There are many comments that could be made about short tags, the version of PHP used, the php.ini file and the rest of it. But it is irrelevant. Just use the long version of the PHP tag
关于短标签、使用的 PHP 版本、php.ini 文件和其余部分,可以进行很多评论。但这无关紧要。只需使用 PHP 标签的长版本
回答by superdario128
I had the same issue. Sometimes it's problem with path.
我遇到过同样的问题。有时是路径问题。
Instead of:
代替:
require_once('foo.php');
Try:
尝试:
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__ . '/your-dir-name/foo.php');

