Autoload function experiences

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
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Autoload function experiences

Post by MindOverBody »

What are your experiences with __autoload function? I am about to start new project and i can not decide will i use autoload function or manual requiring of classes.

I used it in one of my previus project, and it proved that it can realy be pain in the ass whet it comes to class file organization.
Is there a way to implement this function somehow for different cases (class folders)?
So, gimme your opinions and advices, maybe example of useful function body, etc...

Thanks in advance!
Bojan
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Autoload function experiences

Post by alex.barylski »

I use it exclusively, been doing so for years. Initially i was against Zend/PEAR style of naming to ease autoloading and avoid lookup/mapping files but after a lot of experimentation and practical insight, i changed my mind.

I actually find several benefits when using a Zend convention:

1. Prevents duplicate class name declarations from ever occuring
2. Forces you to think about organization of files if you want class names to make sense

It's cleaned up my file structure tremendously, I can easily locate any file within seconds, no more do I spend ages looking for a file or grep'ing, etc. It's also helped make a truly modular/pluggable architecture, where I literaly drop new functionality into a directory and away I go in most cases. At most I run an install script to setup DB tables and default config files.

I think, personally, the jury is out on this one, __autoload() FTW :)

Cheers,
Alex
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: Autoload function experiences

Post by MindOverBody »

I did a lot of reading about this topic yesterday and got some aspects of it's use. I found that many of developers use namespaces and autoload function incorporated as their way of class organisation.

I think i'll go this Zend way, some class name will go wild with length, but thats life :)
Once more, thanks to PCSpectra!

Image
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Autoload function experiences

Post by greyhoundcode »

Apologies for bumping an old (-ish) post, but ...

I like autoloading - mostly I use Kohana which has 1:1 Class:File structure and autoloading (much like ZF I suppose?) - however I would also like to use namespaces.

What thoughts generally do you guys have on autoloading with namespaces, and what would you say are good methods/examples of implementing it? I'm aware of the Lithium Framework (what CakePHP 3 might have been, I believe) though not 100% on how it solves the problem exactly, as I haven't had a chance to dive into its code yet.

What do you think, for example, of mapping only namespaces, not classes, to directories and eliminating the existing Pear/Zend class naming conventions. Like ...

Code: Select all

root/
  application/
    database/

      factory.php          # contains \application\database\factory class for example

      database_driver.php  # contains \application\database\factory\database_driver class,
                            # the underscore in the class name having no relation at all
                            # to its place in the file system 
Pros and cons to this approach?
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: Autoload function experiences

Post by Zyxist »

Autoloading of classes in namespaces is pretty much the same, as autoloading ordinary classes. Namespaces are just a convenience that allows you to shorten some names. If you are interested in using them, I recommend to read the following topic:

http://groups.google.com/group/php-stan ... l-proposal

This is a class naming standard for PHP 5.3+ that is being currently adopted by the biggest PHP projects: Symfony 2, Doctrine 2, Zend Framework 2.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Autoload function experiences

Post by greyhoundcode »

Good link. Nice, simple way of doing it.
dejan
Forum Newbie
Posts: 8
Joined: Tue Dec 21, 2010 6:11 am

Re: Autoload function experiences

Post by dejan »

Just one note: If you are building a library, or coding upon a library, don't use __autoload(), use http://www.php.net/manual/en/function.s ... gister.php instead. In fact, it's a good idea to always use spl_autoload_register.

The usage is like this:

Code: Select all

function custom_autoload($c) { // Whatever }
spl_autoload_register('custom_autoload');
This allows for multiple autoload functions so if you are building a library (or building upon a framework, or whatever), they won't conflict.
Post Reply