To __autoload() or not to __autoload()?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
movedx
Forum Newbie
Posts: 7
Joined: Wed Jun 18, 2008 6:59 am

To __autoload() or not to __autoload()?

Post by movedx »

Hi,

I did a search of the forum to find any topics on the '__autoload()' function, but the results are messy. So I'll ask here: which do you guys prefer, the '__autoload()' method or the including and or requiring(_once) your classes?

I'm also interested to see how you guys implement '__autoload()', for those of you that do use the magic function.

Cheers.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: To __autoload() or not to __autoload()?

Post by alex.barylski »

Personally I have experimented with both, I can't really say one is better than the other.

On one hand, using require/include makes your source code explicit -- no magic which can confuse new developers. On the other hand, an include is something fairly redundant so as long as you make it clear to new developers that classes are included automagically, there should be little problem for new developers.

I don't mean just new developers either. Even in my own projects I take breaks, sometime extended breaks. When I come back to a project months later, if I can still read the code as if I left yesterday, I know it's done right.
movedx
Forum Newbie
Posts: 7
Joined: Wed Jun 18, 2008 6:59 am

Re: To __autoload() or not to __autoload()?

Post by movedx »

I was thinking of having a 'kernel.inc.php' and simply have that have the autoload function in it - two of them, one for read-only classes for "customer facing" pages, and read/write-classes fopr the administration panel - and then just include the kernel into each page requiring the classes.

Thoughts?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: To __autoload() or not to __autoload()?

Post by onion2k »

Autoloading in your own websites is great, and I'd recommend it every time.

If you're writing code that other people will integrate into their sites though... then I'd be a bit more wary of it.
movedx
Forum Newbie
Posts: 7
Joined: Wed Jun 18, 2008 6:59 am

Re: To __autoload() or not to __autoload()?

Post by movedx »

My sites are basically customer's websites. I've done a few websites for sel employed individuals and I want to start writting apps in a more professional manner to make the whole job easier.

Form generation is some what of a boring task for me, and very tedious.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: To __autoload() or not to __autoload()?

Post by Kieran Huggins »

I like __autoload(), but it's really a personal choice.

it can be a nice time saver if you're building a fairly simple app or something modular, but it can also lead to headaches as it adds one more layer of "magic" to doubt when something goes awry.

Try it on, see how it feels. It may shrink during the first wash.
movedx
Forum Newbie
Posts: 7
Joined: Wed Jun 18, 2008 6:59 am

Re: To __autoload() or not to __autoload()?

Post by movedx »

I do try to make my applications/sites as modular as possible. I follow the MVC is my own broken way.

about.class.php = Controller. Contains the 'About_Page' class which acts as a controller for the view, about.php. Then there's about.tmpl which is the template file.

Simple as that really. Can you guys recommend any libs or frameworks that will generate forms based on data that has been passed into it? I don't fancy writing forms out my self manually - it's annoying.

Cheers.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: To __autoload() or not to __autoload()?

Post by Kieran Huggins »

There's currently one hell of a design discussion going on about a form generator. Maybe arborint has some code that could be released separately from the framework in question?
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: To __autoload() or not to __autoload()?

Post by marcth »

I wouldn't use the __autoload() function. Use the spl_autoload_register() function instead--that way you can add several autoload functions in your site if need be.

Marc
Post Reply