2levelsabove Posted February 5, 2010 Share Posted February 5, 2010 Keep getting Fatal error: Class 'someclass' not found in path Even though the path is correct and the files are on server. Please suggest class test{ var $test= ""; function __autoload($class_name) { require_once($_SERVER['DOCUMENT_ROOT']."/includes/_process/". $class_name. ".php"); } function test{ $obj = new someclass; } } Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/ Share on other sites More sharing options...
MadTechie Posted February 5, 2010 Share Posted February 5, 2010 have to confirmed that by eching the path ? Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007154 Share on other sites More sharing options...
trq Posted February 5, 2010 Share Posted February 5, 2010 You have defined __autoload as a method of the test class, it shouldn't be. Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007163 Share on other sites More sharing options...
Hussam Posted February 5, 2010 Share Posted February 5, 2010 try this: function __autoload($class_name) { $class_name = strtolower($class_name); $path = $_SERVER['DOCUMENT_ROOT']."/includes/_process/."{$class_name}.php"; if(file_exists($path)) { require_once($path); } else { die("The file {$class_name}.php doesn't exist."); } } I believe that you have to put this function in a file and include it in each and every class. Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007314 Share on other sites More sharing options...
Mchl Posted February 5, 2010 Share Posted February 5, 2010 I believe that you have to put this function in a file and include it in each and every class. Which would result in 'Cannot redefine function __autoload()' errors all over the place. Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007316 Share on other sites More sharing options...
Hussam Posted February 5, 2010 Share Posted February 5, 2010 Which would result in 'Cannot redefine function __autoload()' errors all over the place. Not really, because a person who is using OOP approach in php should know better and use require_once(''); Anyway even if what you said was true (which is not) he can require it before using any class in a page or using any class in another class (at least). :-) Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007326 Share on other sites More sharing options...
Mchl Posted February 5, 2010 Share Posted February 5, 2010 Anyway even if what you said was true (which is not) he can require it before using any class in a page or using any class in another class (at least). :-) The bottom point is not to try to declare a function more than once. If that's what you mean, than I cannot disagree. Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007332 Share on other sites More sharing options...
Hussam Posted February 5, 2010 Share Posted February 5, 2010 The bottom point is not to try to declare a function more than once. If that's what you mean, than I cannot disagree. Absolutely, I meant if you are concerned about declaring it twice which is not gonna happen if you use require_once('') then just include it where its important. at the end, we both agree that a function can't be declared twice and not a reasonable programmer on this planet can disagree lol, but if you use require_once() then you don't have to worry about that, you can go ahead and do that in all classes and pages, it won't hurt but it might slow the script a little bit to check if its required already or not each and every time. Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007341 Share on other sites More sharing options...
Mchl Posted February 5, 2010 Share Posted February 5, 2010 Actually I would try to layout my application in such a way, that it's always being started from same file (called index.php preferably ). Then that's the only file I need to include/require any such functions into. Much neater than pasting require_once('sameThingAgain.php'); and the beginning of each class file. Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007343 Share on other sites More sharing options...
Hussam Posted February 5, 2010 Share Posted February 5, 2010 Actually I would try to layout my application in such a way, that it's always being started from same file (called index.php preferably ). Then that's the only file I need to include/require any such functions into. Much neater than pasting require_once('sameThingAgain.php'); and the beginning of each class file. I am not sure if I understood exactly what you are trying to say but you have to require this function __autoload() every time you use a class, however if you require the class file itself, then you don't need to, but its always good practice to do that just in case you forgot to require a certain class. Many php developers code their classes in a very complex way to push up all the complexity into the class and make the pages clean, therefor they have alot of classes interacting with each other may be in every class file (creating instances inside each other, inheritance, call their methods statically, ..etc) in this case you might forget to require certain class you used in a certain class file. good luck! Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007352 Share on other sites More sharing options...
Mchl Posted February 5, 2010 Share Posted February 5, 2010 What I mean is: whatever request my application gets from browser, it gets through index.php. This way index.php is the one and only place where I need to include my autoloading functions (I use spl_autoload_* instead of __autoload), or for that matter any other files that are required for my application to work. Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007362 Share on other sites More sharing options...
JAY6390 Posted February 5, 2010 Share Posted February 5, 2010 Mchl is talking about using a bootstrap method (and the MVC architecture presumably) Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007370 Share on other sites More sharing options...
Hussam Posted February 5, 2010 Share Posted February 5, 2010 What I mean is: whatever request my application gets from browser, it gets through index.php. This way index.php is the one and only place where I need to include my autoloading functions True, that will work too, but I meant to answer his question which is not related to what your talking about, may be he is not using this approach. Anyway, lets end this debate lol, you got me wrong when I said include the function file and thought of the word "include" as a php function in php language not as a word in English language otherwise you won't be telling me about the redefining the function error problem, that's what started all this lol. cheers! Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007373 Share on other sites More sharing options...
Mchl Posted February 5, 2010 Share Posted February 5, 2010 I meant to answer his question which is not related to what your talking about, may be he is not using this approach. Seeing the code snippet in the first post, he's just starting toying with OOP and autoloading. No harm in giving some directions I suppose Anyway, lets end this debate lol, you got me wrong when I said include the function file and thought of the word "include" as a php function in php language not as a word in English language otherwise you won't be telling me about the redefining the function error problem, that's what started all this lol. Indeed... include is include for me Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007377 Share on other sites More sharing options...
Hussam Posted February 5, 2010 Share Posted February 5, 2010 Yes absolutely, this is how people learn, and it was good suggestion from you to follow the way you mentioned but new developers toying with OOP might find it a little bit confusing at first. For me I use "include" as an English word and "include()" as a function . cheers! Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007382 Share on other sites More sharing options...
ignace Posted February 5, 2010 Share Posted February 5, 2010 Which would result in 'Cannot redefine function __autoload()' errors all over the place. Not really, because a person who is using OOP approach in php should know better and use require_once(''); What Mchl said is TRUE that Guru thingie actually means something and if you were to use OOP then you wouldn't even bother using __autoload but you would be smart and be using spl_autoload_register I am not sure if I understood exactly what you are trying to say but you have to require this function __autoload() every time you use a class, however if you require the class file itself, then you don't need to, but its always good practice to do that just in case you forgot to require a certain class. Then why would you use __autoload in the first place? I generally declare a spl_autoload_register class and then leave the require_once out in class files Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007475 Share on other sites More sharing options...
Mchl Posted February 5, 2010 Share Posted February 5, 2010 igance: We've already settled this issue down Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007476 Share on other sites More sharing options...
2levelsabove Posted February 5, 2010 Author Share Posted February 5, 2010 wow this discussion really picked up. Any how I decided to not use __autoload and just use the old fashioned way of including classes. Reason being I am using a CMS system that already has its own directory of classes and I am keeping my custom classes in a different folder. If I use __autoload, than the darn thing starts looking for classes in the wrong path and then it cannot find classes for the CMS systems classes. Maybe I can do an if else struct in the __autoload to look in both folders. One thing I do find interesting is that even cool CMS systems like WordPress do not use __autoload etc Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007479 Share on other sites More sharing options...
Mchl Posted February 5, 2010 Share Posted February 5, 2010 Maybe I can do an if else struct in the __autoload to look in both folders. You most certainly can One thing I do find interesting is that even cool CMS systems like WordPress do not use __autoload etc WP suffers from more design issues that that. Quote Link to comment https://forums.phpfreaks.com/topic/190991-auto-loading-help-in-oop/#findComment-1007485 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.