Page 1 of 1

Autoload function experiences

Posted: Wed Aug 25, 2010 9:09 pm
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

Re: Autoload function experiences

Posted: Thu Aug 26, 2010 9:37 am
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

Re: Autoload function experiences

Posted: Fri Aug 27, 2010 12:38 am
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

Re: Autoload function experiences

Posted: Thu Dec 16, 2010 1:43 am
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?

Re: Autoload function experiences

Posted: Fri Dec 17, 2010 1:10 am
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.

Re: Autoload function experiences

Posted: Fri Dec 17, 2010 12:49 pm
by greyhoundcode
Good link. Nice, simple way of doing it.

Re: Autoload function experiences

Posted: Wed Dec 22, 2010 2:50 am
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.