找不到PHP类

9 php module class content-management-system

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

大家好!

我正在构建像Drupal和Joomla这样的CMS.我正在研究模块功能(插件),我收到以下错误:

Fatal error: Class 'settings' not found in C:\wamp\www\SYSTEM\view.php on line 22
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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']);
    }
}
Run Code Online (Sandbox Code Playgroud)

的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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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");
Run Code Online (Sandbox Code Playgroud)

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

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

dna*_*irl 30

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

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

第二种选择更灵活.

如果您想知道某个地方的课程可以尝试输出 get_declared_classes()

  • +1 为 print_r(get_declared_classes()) 建议 (2认同)

Kon*_*tin 7

以下不适用于OP的情况,但可能会帮助其他人.

检查您的代码是否使用短标签<?而不是,<?php如果是,请检查您的php.ini进行short_open_tag设置.

默认情况下它是关闭但如果您从其他人继承您的PHP安装...