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> Quote Link to comment Share on other sites More sharing options...
requinix Posted July 25, 2013 Share Posted July 25, 2013 (edited) __autoload is deprecated. Use spl_autoload_register instead. "In a different folder"? You mean you have more than one ClassAppTitle class defined? Edited July 25, 2013 by requinix Quote Link to comment Share on other sites More sharing options...
Yohanne Posted July 25, 2013 Author Share Posted July 25, 2013 yes i have. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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... Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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(); Quote Link to comment 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. Quote Link to comment 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.