【问题标题】:PHP class not found [closed]找不到PHP类[关闭]
【发布时间】:2010-11-13 20:36:02
【问题描述】:

我自己解决了这个问题。文件名错误lolz。

大家好!

我正在构建一个像 Drupal 和 Joomla 这样的 CMS。我正在开发模块功能(插件),但出现以下错误:

Fatal error: Class 'settings' not found in C:\wamp\www\SYSTEM\view.php on line 22

这是我的代码:

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
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
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 显然是先执行的。除了包含数据库信息的 customsettings.php 之外,不会执行其他页面。如果我在上面的代码中使用了 $_SERVER["db_prefix"],那是因为我需要一个在 customsettings.php 中设置的超全局:

customsettings.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

有人可以帮我吗?似乎在包含 settings.php 之前调用了 view.php 的索引函数。抱歉,如果这个问题很大,我只想说清楚。也不要说 eval() 是邪恶的,我知道。

所以我想知道为什么找不到设置类。如果您需要更多源代码,请对此问题发表评论。

【问题讨论】:

  • 虽然很愚蠢,但我也遇到了命名问题。忘记了.php。

标签: php class content-management-system module


【解决方案1】:

虽然您希望 settings.php 可用于 view.php,因为它包含在包含它们两者的脚本中,但我发现通常情况并非如此。您有两种选择:

  • require_once每个类文件中每个类需要的所有文件
  • 编写一个__autoload() 函数,以便PHP 可以在它认为需要时找到所有类

第二个选项更灵活。

如果您想知道某个特定地点的课程是否可用,请尝试输出 get_declared_classes()

【讨论】:

  • 对不起,即使将 settings.php 包含到 view.php 中也不起作用:S
  • 我知道我做错了什么。我的文件名是错误的。洛兹
  • +1 用于 print_r(get_declared_classes()) 建议
【解决方案2】:

以下内容不适用于 OP,但可能对其他人有所帮助。

检查您的代码是否使用短标签 &lt;? 而不是 &lt;?php,如果是,则检查您的 php.ini 中的 short_open_tag 设置。

默认情况下它是关闭的,但如果你从别人那里继承你的 php 安装...

【讨论】:

  • 如果移动到 php.ini 文件可能具有不同设置的新服务器,这很容易发生 - 很好 @Konstantin。
【解决方案3】:

万一有人偶然发现这个问题,我也遇到了这个问题,我通过确保 php 文件的名称与实际文件中的 php 类的名称相同来解决它。

傻,我知道。

【讨论】:

  • 文件名不一定重要。这可能取决于您的自动加载器。
  • 嗯,很奇怪。谢谢你让我知道。无论如何,我很肯定这为我解决了问题。无论默认设置是什么,我都在使用 Eclipse。
【解决方案4】:

可能会出现另一个问题,值得任何人了解。如果你使用 __autoload() 并且在包含被自动加载的类的文件中你写错了你的 PHP 标签,它将返回一个类未找到错误:

文件 App.php

<?

class App extends something
{
    function __construct()
    {

    }
}
?>

文件 index.php

<?php

function __autoload($classname) {

    $filename = "./classes/". $classname .".php";
    print("Loading $filename<br>\n");
    include_once($filename);

}


$app = new App();

?>

上面的代码不起作用。为了让它工作,你需要用一个长标签替换短的 PHP 标签 App.php:

<?php

class App extends something
{
    function __construct()
    {

    }
}
?>

关于短标签、使用的 PHP 版本、php.ini 文件和其他内容,可以创建许多 cmets。但这无关紧要。只需使用 PHP 标记的长版本

【讨论】:

    【解决方案5】:

    我有同样的问题。有时是路径问题。

    代替:

    require_once('foo.php');
    

    试试:

    define('__ROOT__', dirname(dirname(__FILE__)));
    require_once(__ROOT__ . '/your-dir-name/foo.php');
    

    【讨论】:

      猜你喜欢
      • 2014-06-16
      • 2018-11-22
      • 2021-02-14
      • 2017-11-25
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 2010-10-25
      • 2018-04-06
      相关资源
      最近更新 更多