Jump to content

[SOLVED] Autoload function not working


lordzardeck

Recommended Posts

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)

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

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.

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?

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?).

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?

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)

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.

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.

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();
          
      }
  }

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();
          
      }
  }
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.