Jump to content

Archived

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

lordzardeck

[SOLVED] Autoload function not working

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)

Share this post


Link to post
Share on other sites

You are not really calling your function.

 

Try:

classAutoload('classAutoload');

 

It should work.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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?

Share this post


Link to post
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?).

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

I think there is no __autoload() functionality at all in PHP4

Share this post


Link to post
Share on other sites

can a class name when being declared be a variable? For example:

 

$page =  PageHandler::get_page();
          
$content = new $page();

Share this post


Link to post
Share on other sites

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.

Share this post


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

Share this post


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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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