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

I've given a try by moving to __construct but no luck. Could you please explain with small demo?

 

Edit: I think i got it working :) Could you explain me a method so that I don't need to keep writing new ClassName everytime?

Edited by stalkmadude
Link to comment
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
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.