mrooks1984 Posted November 15, 2011 Share Posted November 15, 2011 hello, all i am trying to learn php, and currently trying to get my head around using classes. i have the following layout: index.php <?php include("config/autoloader.php"); $content = new content(); $content->get_content(); // loads Content! $content->edit_content(); // Edits Content! ?> <?php $navbars = new navbars(); $navbars->get_navbar1(); // loads navbar1! ?> config/autoloader.php <?php function spl_register_autoload($content) { include_once("classes/" . $content . ".php"); } function spl_register_autoload($navbars) { include_once("classes/" . $navbars . ".php"); } ?> content.php (content class file) <?php class content { public function get_content() { echo "Show me the content!"; } public function edit_content() { echo "Edit the content"; } } ?> navbars.php navbars class file <?php class navbars { public function get_navbar1() { echo "Show me the navbar1!"; } } ?> i keep getting the 500 - internal error on iis, but works on a single class file, as soon as i add more then one i get the error, can someone please help me and explain where i have gone wrong. thanks, all. Quote Link to comment https://forums.phpfreaks.com/topic/251215-php-autoload-classes/ Share on other sites More sharing options...
xyph Posted November 15, 2011 Share Posted November 15, 2011 I'm not sure why you're getting a 500 error. I know that you should be getting a fatal error trying to create the same function twice in autoloader.php Quote Link to comment https://forums.phpfreaks.com/topic/251215-php-autoload-classes/#findComment-1288493 Share on other sites More sharing options...
mrooks1984 Posted November 15, 2011 Author Share Posted November 15, 2011 thanks for your responce, so can you tell me what i need to do to make the autoloader, work? Quote Link to comment https://forums.phpfreaks.com/topic/251215-php-autoload-classes/#findComment-1288497 Share on other sites More sharing options...
phporcaffeine Posted November 15, 2011 Share Posted November 15, 2011 You can't declare two functions/methods with the same name, in the same runtime namespace. You have to make each function name unique. However, within classes, you can use the same method name as long as they are in separate classes - but I wouldn't recommend that; there is no reason that every function and method can't have there own name. Quote Link to comment https://forums.phpfreaks.com/topic/251215-php-autoload-classes/#findComment-1288503 Share on other sites More sharing options...
requinix Posted November 15, 2011 Share Posted November 15, 2011 You're also misunderstanding how spl_autoload_register() works. Read through the user comments on the PHP manual page for examples how to use it. Quote Link to comment https://forums.phpfreaks.com/topic/251215-php-autoload-classes/#findComment-1288516 Share on other sites More sharing options...
xyph Posted November 15, 2011 Share Posted November 15, 2011 However, within classes, you can use the same method name as long as they are in separate classes - but I wouldn't recommend that; there is no reason that every function and method can't have there own name. Disagree here. Using similar names for similar methods saves you from having to remember different naming schemes. Quote Link to comment https://forums.phpfreaks.com/topic/251215-php-autoload-classes/#findComment-1288521 Share on other sites More sharing options...
phporcaffeine Posted November 16, 2011 Share Posted November 16, 2011 However, within classes, you can use the same method name as long as they are in separate classes - but I wouldn't recommend that; there is no reason that every function and method can't have there own name. Disagree here. Using similar names for similar methods saves you from having to remember different naming schemes. Tamato / tomato ..... True, there is a good consistency argument to be made here, however, can present some confusion for someone just starting to learn OOP. Quote Link to comment https://forums.phpfreaks.com/topic/251215-php-autoload-classes/#findComment-1288534 Share on other sites More sharing options...
xyph Posted November 16, 2011 Share Posted November 16, 2011 If they can't debug a duplicate function declaration, OOP isn't something they're ready for, IMO. Quote Link to comment https://forums.phpfreaks.com/topic/251215-php-autoload-classes/#findComment-1288548 Share on other sites More sharing options...
mrooks1984 Posted November 16, 2011 Author Share Posted November 16, 2011 i do have a small amount of knowledge on functions and them not been able to the same name, i have been doing my functions in one file. the issue is my class file is getting rather big and hard to update and work with, so i wanted to try and find out how to seperate functions into different files and have them in a folder to only load when needed to save on resources and easy for me to manage and add too. i have looked at the said page before i posted on here, but i cant see or understand how i would use the autoloader to load all the different classes from each file and from what i can see it, they only show 1 in the examples (if this is the wrong way pplease tell me). which when i looked at a article it said this is the best way of doing things. i have googled it looked at multiple websites etc etc but have not had any explanation on how best to do this. so i was hoping i would of more luck here. thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/251215-php-autoload-classes/#findComment-1288613 Share on other sites More sharing options...
KevinM1 Posted November 16, 2011 Share Posted November 16, 2011 If you're using classes simply to group functions by theme, you're not doing OOP. You're only adding unneeded complexity to what could easily be solved simply by having pure function library files. OOP is something that should not be attempted until you have a firm grasp of the fundamentals. Otherwise you'll just end up confusing yourself. If you insist on using spl_autoload_register, here's how you'd do it: function myLoader($className) { include_once("classes/" . $className . ".php"); } spl_autoload_register("myLoader"); Quote Link to comment https://forums.phpfreaks.com/topic/251215-php-autoload-classes/#findComment-1288656 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.