stalkmadude Posted March 9, 2014 Share Posted March 9, 2014 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! Quote Link to comment https://forums.phpfreaks.com/topic/286832-spl_autoload_register-and-class/ Share on other sites More sharing options...
sKunKbad Posted March 9, 2014 Share Posted March 9, 2014 You can't use a standard function inside a class like that. What I mean is, the call to the function isn't inside a method. If you want it to run automatically, put the call inside a __construct method. Quote Link to comment https://forums.phpfreaks.com/topic/286832-spl_autoload_register-and-class/#findComment-1471952 Share on other sites More sharing options...
stalkmadude Posted March 9, 2014 Author Share Posted March 9, 2014 (edited) 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 March 9, 2014 by stalkmadude Quote Link to comment https://forums.phpfreaks.com/topic/286832-spl_autoload_register-and-class/#findComment-1471956 Share on other sites More sharing options...
Yohanne Posted March 10, 2014 Share Posted March 10, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/286832-spl_autoload_register-and-class/#findComment-1471959 Share on other sites More sharing options...
stalkmadude Posted March 10, 2014 Author Share Posted March 10, 2014 Thanks Jayson for the help, WIth that would I be able to avoid writing new for each classes? Quote Link to comment https://forums.phpfreaks.com/topic/286832-spl_autoload_register-and-class/#findComment-1471969 Share on other sites More sharing options...
Yohanne Posted March 10, 2014 Share Posted March 10, 2014 Yes you can by adding. $new_value = new ClassName(); $new_value = new ClassName1(); $new_value = new ClassName2(); Quote Link to comment https://forums.phpfreaks.com/topic/286832-spl_autoload_register-and-class/#findComment-1471980 Share on other sites More sharing options...
stalkmadude Posted March 10, 2014 Author Share Posted March 10, 2014 That's not what I meant I meant I want to avoid typing the above code for every new class, something like Zend Framework. Quote Link to comment https://forums.phpfreaks.com/topic/286832-spl_autoload_register-and-class/#findComment-1471983 Share on other sites More sharing options...
trq Posted March 10, 2014 Share Posted March 10, 2014 Do yourself a favour and just use composer and it's autoloader like every other programmer writing modern code. http://getcomposer.org Quote Link to comment https://forums.phpfreaks.com/topic/286832-spl_autoload_register-and-class/#findComment-1471986 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.