PHP class not found

I solved this issue myself. The file name was incorrect lolz.

Hello to all!

I am creating a CMS like Drupal and Joomla. I am working on a module function (plugins) and I got the following error:

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 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 is obviously executed first. No other pages are run before it, with the exception of customettings.php, which contains information about the database. If I used $ _SERVER ["db_prefix"] in my code above, this is because I need a superglobal that is set in customettings.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 

Can someone help me? It seems that the index.php index function is called before the settings.php parameter is enabled. Sorry if this question is huge, I just want to be clear. Also don't say that eval () is evil, I know.

So, I want to know why the settings class is not found. If you need more source code, comment on this question.

+7
php module class content-management-system
source share
5 answers

Although you would expect settings.php to be available for view.php since it was included in a script that includes both of them, I found that this is usually not the case. You have several options:

  • require_once all files needed by each class in each class file
  • write the __autoload() function so that PHP can find all your classes whenever it thinks it is needed.

The second option is more flexible.

If you want to know that classes are accessible from a specific place, try getting get_declared_classes()

+29
source share

In the case of OP, the following does not apply, but may help others.

Check if your code uses short tags <? instead of <?php , and if so, check the php.ini settings for short_open_tag .

It is turned off by default, but if you inherit your php installation from someone else ...

+7
source share

Just in case someone came across this question, I also had this problem, and I solved it by making sure that the php file name matches the php class name inside the actual file.

Stupid, I know.

+4
source share

There is another problem that may arise, and someone should know it. If you use __autoload (), and in the file that contains the autoload of the class, you are writing your PHP tags incorrectly, it will return an error not found by the class:

App.php File

 <? class App extends something { function __construct() { } } ?> 

file index.php

 <?php function __autoload($classname) { $filename = "./classes/". $classname .".php"; print("Loading $filename<br>\n"); include_once($filename); } $app = new App(); ?> 

The code above does not work. For it to work, you need to replace the short PHP PHP file App.php with the long one:

 <?php class App extends something { function __construct() { } } ?> 

There are many comments that can be made about short tags, the version of PHP used, the php.ini file, and the rest. But it does not matter. Just use the long version of the PHP tag

0
source share

I had the same problem. Sometimes this is a circuit problem.

Instead:

 require_once('foo.php'); 

Try:

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

All Articles