lordzardeck Posted February 21, 2009 Share Posted February 21, 2009 I get this error when trying to autoload classes: Fatal error: Call to undefined function: spl_autoload_register() in /homepages/36/d134836550/htdocs/test/admin/classes/autoload.php on line 9 Here is my page: <?php function classAutoload($class_name) { require_once($class_name.".php"); } spl_autoload_register('classAutoload'); ?> Anyone know what the problem is? (i hope its not staring me in the face) Quote Link to comment Share on other sites More sharing options...
angelcool Posted February 21, 2009 Share Posted February 21, 2009 You are not really calling your function. Try: classAutoload('classAutoload'); It should work. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 21, 2009 Share Posted February 21, 2009 Don't use spl_autoload_register(), just name the function __autoload() <?php function __autoload($class_name) { require_once($class_name.".php"); } It's likely you're using a version of php pre-spl_autoload_register() edit: apparently I can't spell autoload Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 You are not really calling your function. Try: classAutoload('classAutoload'); It should work. But it's not supposed to be called unless the class is not already loaded. Right? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 21, 2009 Share Posted February 21, 2009 You are not really calling your function. Try: classAutoload('classAutoload'); It should work. But it's not supposed to be called unless the class is not already loaded. Right? Yes, I think he misunderstood your question. Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 Don't use spl_autoload_register(), just name the function __autoload() <?php function __autoload($class_name) { require_once($class_name.".php"); } It's likely you're using a version of php pre-spl_autoload_register() edit: apparently I can't spell autoload I tried using it, but it doesn't load it. Is it case sensitive? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 21, 2009 Share Posted February 21, 2009 What version of php are you using? How are you using the autoload function? <?php function __autoload($class_name) { require_once($class_name.".php"); } MyClass::someFunction(); // or $myclass = new MyClass(); ? And what do you mean is it case sensitive? the function or the class name? The class name is case sensitive on unix (maybe on windows too?). Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 What version of php are you using? How are you using the autoload function? <?php function __autoload($class_name) { require_once($class_name.".php"); } MyClass::someFunction(); // or $myclass = new MyClass(); ? And what do you mean is it case sensitive? the function or the class name? The class name is case sensitive on unix (maybe on windows too?). First off, I declare it like this: $myclass = new MyClass(); Second off, what I mean is, can the file name be myclass.php, but I declare it as MyClass? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 21, 2009 Share Posted February 21, 2009 You will need to do <?php function __autoload($class_name) { require_once(strtolower($class_name).".php"); } if you want to do it that way. (On unix at least, not sure about windows, windows tends to not be case sensitive with file names) Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 21, 2009 Share Posted February 21, 2009 Which PHP version do you use? SPL functions are enabled by default in PHP5. By default __autoload() function is case insensitive. If you register your own, you probably need to take care of it. On windows it doesn't care avout case of course. Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 it must be at least php5 because I had a wiki using php5 before. if it was php4, how would I know? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 21, 2009 Share Posted February 21, 2009 make a page.. <?php phpinfo(); would be the easiest way. __autoload() is case sensitive with class names on unix though because unix is case sensitive with file names. Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 I still get this error: Fatal error: Cannot instantiate non-existent class: menu in /homepages/36/d134836550/htdocs/test/admin/classes/pagehandler.php on line 51 Edit**** I have tried the last post yet Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 Okay, I found out that it's 4.4.9. Dang, wonder why 1&1 didn't update? So is that a problem? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 21, 2009 Share Posted February 21, 2009 Your host may have an option to use php5, most shared hosts have a toggle where you can choose between php4 and php5. Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 Does not having php5 keep me from using autoloading features? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 21, 2009 Share Posted February 21, 2009 Yes, the differences between php4 and php5 are numerous, especially when it comes to OOP. OOP was completely revamped in php5. Like I said, look around in your admin panel, there will be an option to switch to php5. Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 21, 2009 Share Posted February 21, 2009 I think there is no __autoload() functionality at all in PHP4 Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 I had to edit a .htaccess file, but it's now 5.2.8! Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 can a class name when being declared be a variable? For example: $page = PageHandler::get_page(); $content = new $page(); Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 21, 2009 Share Posted February 21, 2009 And hopefully you won't have any more problems And yes, you can do that lordzard, but it's kind of confusing to whoever is reading your code. Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 well, now i'm getting this error: Fatal error: Class name must be a valid object or a string in /homepages/36/d134836550/htdocs/test/admin/classes/pagehandler.php on line 93 line 93 and the surrounding lines are: function add_content(){ $page = PageHandler::get_page(); $content = new $page(); $content->build_page(); } } Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 21, 2009 Share Posted February 21, 2009 Can you show us the code for PageHandler::get_page()? it's likely that it's not returning a string. Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 Here is the entire page: <?php require_once("./classes/autoload.php"); //start page render class class PageHandler{ function page_build(){ PageHandler::add_header(); PageHandler::load_styles(); PageHandler::end_header(); PageHandler::add_body(); PageHandler::add_menu(); PageHandler::add_content(); PageHandler::end_page(); } function add_header(){ print "<html>"; print "\n<head>\n<title>SAC Online Catalog</title>"; } function end_header(){ print "\n</head>"; } function add_body(){ print "\n<body>"; } function end_page(){ print "\n</body>\n</html>"; } function add_menu(){ if(!$menu){ $menu = new Menu(); }else{ $menu->menu_build(); } } function load_styles(){ $css_dir = opendir('./css'); while($file = readdir($css_dir)){ if($file != "." AND $file != ".."){ print "\n<link rel='stylesheet' type='text/css' href='$file' media='all' />"; } } closedir($css_dir); } function set_page($function_page_load){ $page_load = $function_page_load; } function get_page(){ return $page_load; } function add_content(){ $page = PageHandler::get_page(); $content = new $page(); $content->build_page(); } } ?> Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 21, 2009 Share Posted February 21, 2009 <?php function get_page(){ return $page_load; } $page_load isn't set because it's a local variable, if you mean to address the class's member variable you'd need to do <?php function get_page(){ return $this->page_load; } If you turn on E_ALL errors, it will help you debug these types of issues. It looks like all of your code is made for php4 as none of the functions have access modifiers or are set as static. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.