alexweber15 Posted September 2, 2008 Share Posted September 2, 2008 been reading about the wonders of __autoload and spl_autoload and they sound great specially if you have lots of classes.... two questions: 1) is it case-sensitive? function __autoload ($className) { require_once ($className.'.class.php'); } so for example i need a class called "Product" but the source code is in a file called "product.class.php"... will this work? or should i name my .class.php files in the same case as the actually class declaration? 2) WHERE do i define it???? in the first .php file that gets executed? (index.php) or what? assume i use a 3 layer design, should i define it at the top of the business/logic layer? thanks... ! -Alex Quote Link to comment Share on other sites More sharing options...
trq Posted September 2, 2008 Share Posted September 2, 2008 1. Yes its case sensitive. 2. Somewhere within scope. Usually within your applications first entry point. Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 2, 2008 Share Posted September 2, 2008 I think it's better to use spl_autoload_register(). For example, I have this code function classAutoload($class_name) { require_once("class.".$class_name.".php"); } spl_autoload_register('classAutoload'); in a file called autoload.php, which I require at the beginning of each php file that might use these classes. Quote Link to comment Share on other sites More sharing options...
alexweber15 Posted September 3, 2008 Author Share Posted September 3, 2008 I think it's better to use spl_autoload_register(). For example, I have this code function classAutoload($class_name) { require_once("class.".$class_name.".php"); } spl_autoload_register('classAutoload'); in a file called autoload.php, which I require at the beginning of each php file that might use these classes. correct me if i'm wrong but isn't the exact same thing as I posted at the start of the thread? AFAIK spl_autoload() is basically an alias to __autoload() if you don't change any parameters or behaviors via the other sp_autoload_etc....() functions... also calling it every time you might use a function kinda defeats the purpose IMO.... in fact, you just basically created a function that does the exact same thing as __autoload() originally does and used spl_autoload_register to overwrite the __autoload() function cache... just did some reading up on the manual: http://www.php.net/manual/en/function.spl-autoload.php void spl_autoload ( string $class_name [, string $file_extensions ] ) By default it checks all include paths to contain filenames built up by the lowercase class name appended by the filename extensions .inc and .php. so if i understood this properly: 1) its NOT case sensitive 2) the advantage of using spl_autoload() instead of __autoload() is that you can customize it further: Example: ; spl_autoload($classname,'.class.php'); Again, please correct me if i'm wrong, but inserting the above line anywhere in your code will make sure that if you try to create an instance of $classname not included/required anywhere in your code it will look in your include_path for matching files with $classname."class.php" ... right? thus eliminating the need to explicitly create the __autoload() function and with no other code necessary? thx -Alex Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 3, 2008 Share Posted September 3, 2008 Advantage of spl_autoload is that you can redefine it (or in fact have several autoload functions). My example doesn't use this feature of course, although it helped me get rid of 'Can't redefine __autoload()' warning I was getting. As for the case sensitivity, I believe that manual implies it's not case sensitive by default, but on the arguments' side not the files' side. In example I posted before, I use classes named like dbClient and store them in files like: class.dbclient.php and it works well I'm on Windows, so checking if it would work with: class.dbClient.php , or: class.DBCLIENT.php is pointless... Quote Link to comment Share on other sites More sharing options...
alexweber15 Posted September 3, 2008 Author Share Posted September 3, 2008 duly noted! thanks! (gonna hold off on using this feature for now) 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.