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) Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/ 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767951 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 Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767952 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? Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767953 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767954 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? Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767955 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?). Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767960 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? Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767965 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) Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767967 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767971 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? Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767973 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767974 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 Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767975 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? Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767979 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767982 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? Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767984 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767986 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 Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767990 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! Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767992 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(); Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767994 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767996 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(); } } Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767997 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767998 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(); } } ?> Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-767999 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/#findComment-768000 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.