Jump to content

where to define __autoload() function???


alexweber15

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.