Yohanne Posted July 25, 2013 Share Posted July 25, 2013 any idea how to call autoload class in different folder? ex. below that i have.. auto.php <?php require 'login/index.php'; function __autoload($classname) { $filename = "./../class/". $classname .".php"; include_once($filename); } ?> login/index.php <?php $new_apptitle = new ClassAppTitle(); ?> <html> <head> <title><?php $new_apptitle->get_apptitle(); ?></title> </head> <body> </body> </html> now, how do i re-call the $new_apptitle = new ClassAppTitle(); into different folder? let say like below.. tab/home.php <?php $new_apptitle = new ClassAppTitle(); ?> <html> <head> <title><?php $new_apptitle->get_apptitle(); ?></title> </head> <body> </body> </html> Link to comment https://forums.phpfreaks.com/topic/280481-re-call-autoload-class-in-different-folder/ Share on other sites More sharing options...
requinix Posted July 25, 2013 Share Posted July 25, 2013 __autoload is deprecated. Use spl_autoload_register instead. "In a different folder"? You mean you have more than one ClassAppTitle class defined? Link to comment https://forums.phpfreaks.com/topic/280481-re-call-autoload-class-in-different-folder/#findComment-1442028 Share on other sites More sharing options...
Yohanne Posted July 25, 2013 Author Share Posted July 25, 2013 yes i have. Link to comment https://forums.phpfreaks.com/topic/280481-re-call-autoload-class-in-different-folder/#findComment-1442031 Share on other sites More sharing options...
trq Posted July 25, 2013 Share Posted July 25, 2013 Do yourself a favour and follow a standard. https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md You can then use any number of implementation that already exist including composers auto loader. Though to be fair, you can use composers auto loader without being PSR-0 compliant, but anyway. Link to comment https://forums.phpfreaks.com/topic/280481-re-call-autoload-class-in-different-folder/#findComment-1442033 Share on other sites More sharing options...
Yohanne Posted July 25, 2013 Author Share Posted July 25, 2013 i get lost the example that you recommend but by the way, could you elaborate more, or give me an example like structure above, in use of spl_autoload_regester. Link to comment https://forums.phpfreaks.com/topic/280481-re-call-autoload-class-in-different-folder/#findComment-1442036 Share on other sites More sharing options...
requinix Posted July 25, 2013 Share Posted July 25, 2013 Your first problem is having more than one class with the same name. That's a no-no. Name each class differently because they are, in fact, different classes. Then rename their files appropriately and if necessary modify your autoloader to fit, since it now knows which classes you're talking about. Link to comment https://forums.phpfreaks.com/topic/280481-re-call-autoload-class-in-different-folder/#findComment-1442037 Share on other sites More sharing options...
Yohanne Posted July 25, 2013 Author Share Posted July 25, 2013 Okay thanks CODERS and everyOne, now i get the logic behind spl_autoload... Link to comment https://forums.phpfreaks.com/topic/280481-re-call-autoload-class-in-different-folder/#findComment-1442041 Share on other sites More sharing options...
trq Posted July 25, 2013 Share Posted July 25, 2013 i get lost the example that you recommend but by the way, could you elaborate more, or give me an example like structure above, in use of spl_autoload_regester. There is an example on the page I linked you to. It's also a very well documented spec, where exactly are you lost? Link to comment https://forums.phpfreaks.com/topic/280481-re-call-autoload-class-in-different-folder/#findComment-1442051 Share on other sites More sharing options...
trq Posted July 25, 2013 Share Posted July 25, 2013 Your first problem is having more than one class with the same name. That's a no-no. Name each class differently because they are, in fact, different classes. Then rename their files appropriately and if necessary modify your autoloader to fit, since it now knows which classes you're talking about. This is a non issue thanks to namespaces. Link to comment https://forums.phpfreaks.com/topic/280481-re-call-autoload-class-in-different-folder/#findComment-1442052 Share on other sites More sharing options...
requinix Posted July 25, 2013 Share Posted July 25, 2013 This is a non issue thanks to namespaces.*more than one class with the same fully-qualified name Link to comment https://forums.phpfreaks.com/topic/280481-re-call-autoload-class-in-different-folder/#findComment-1442060 Share on other sites More sharing options...
Yohanne Posted July 27, 2013 Author Share Posted July 27, 2013 Hi, i get same issue regarding spl_autoload_regester. it did not load file data. why? class Action { public static function systemtitle($className) { $dataname = "../class/" .$className. ".php"; require $dataname; } public static function dataParameter($classNamee) { $datanamee = "../data/" .$classNamee. ".php"; require $datanamee; } } spl_autoload_register('Action::systemtitle'); $datavaluee = new ClassData(); $datavaluee->classdatas(); spl_autoload_register('Action::dataParameter'); $datavaluee = new parameter(); $datavaluee->parameters(); Link to comment https://forums.phpfreaks.com/topic/280481-re-call-autoload-class-in-different-folder/#findComment-1442361 Share on other sites More sharing options...
requinix Posted July 27, 2013 Share Posted July 27, 2013 Are you sure you're not getting a "failed to open stream" error with require()? Because you should be getting at least one and that will kill your script. Have you tried even the most basic of debugging techniques: printing stuff out and seeing what's happening? Because you're in a much better position to do that then we are, given that it's your code we're trying to help you with. Link to comment https://forums.phpfreaks.com/topic/280481-re-call-autoload-class-in-different-folder/#findComment-1442364 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.