Jump to content

spl_autoload_register and class


stalkmadude

Recommended Posts

Hello,

 

I'm having hard time with spl_autoload_register function specially when its inside a class. I would appreciate any help :)

 

This is my index.php

<?php
require('./loadclasses.php');
?>

loadclasses.php

<?php
class loadclasses
{
    function autoLoader($className)
    {
        $fileName = str_replace("_", PATH_SEPARATOR, $className);

        include($fileName);
    }

    spl_autoload_register('autoLoader');
}

But i keep getting this error

Parse error: syntax error, unexpected 'spl_autoload_register' (T_STRING), expecting function (T_FUNCTION) in d:\xampp\htdocs\welcomemanager\loadclasses.php on line 11

 

I am running php 5.4 version on xmapp

 

thanks again!

Link to comment
https://forums.phpfreaks.com/topic/286832-spl_autoload_register-and-class/
Share on other sites

Hi.

 

to your index put this code to autoload all your your classis and you dont need to use require tag. hope it helps.

<?php
	
  class loadclasses
	{
	   public static function autoLoader($className)
		{
		  $value = '/foldername/foldername1/' .$className. '.php';  require $value; 
		}	
	}
  spl_autoload_register('loadclasses::autoLoader');  $new_value = new ClassName(); $new_value->ClassProperty();

<?php

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.