Jump to content

Auto Loading help in OOP


2levelsabove

Recommended Posts

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;
}
}



Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

 

:-)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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  8).

 

cheers!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.