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)

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

<?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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.