Jump to content

Can someone explain spl_autoload_register setup to me that makes sense?


2levelsabove

Recommended Posts

Ok so I know what spl_autoload_register does. Basically you are setting up a structure to where classes get called automatically as they are needed. However I have not seen a workable practical example.

 

Lets say I have an application that can call any number of classes:

 

at the top of the application I put:

 

require_once(dirname(__FILE__) .'/http_autoloader.php'); //helps me autoload classes

 

The contents of my http_autoloader.php are:

 

function __autoload_HTTP_Client($class_name){
$HC = dirname(__FILE__).'/classA.php';
require_once($HC);
return true;
}

spl_autoload_register('__autoload_HTTP_Client');

 

The problem I am having is that what if my code requires some other class? How do I incorporate that into spl_autoload_register? I just need help understanding it in a simpler fasion.

 

Thanks guys (and yes I have Googled this and the examples are awful)

 

 

 

Link to comment
Share on other sites

Notice how your autoload function needs to accept a classname? You need to use that to load your classes. eg;

 

function __autoload_HTTP_Client($class_name) {
  $HC = '/path/to/classes' . $class_name . '.php';
  return require_once($HC);
}

spl_autoload_register('__autoload_HTTP_Client');

 

Now, that's all well and good providing all your classes are within /path/to/classes. However, most people don't want to jam all there classes in one directory like that. Thats when you need to decide on a naming convention that works along side a convention defining where your classes need to be. PEAR has a popular conventional already defined (see here'). Allot of projects, and individuals (including myself) use a similar naming convention. It basically works like this.....

 

Given this directory structure.

|-- library
|   `-- Libname
|       |-- Application.php
|       |-- Bootstrap
|       |   `-- Resource
|       |-- Event
|       |   |-- Chain
|       |   |   |-- EventAbstract.php
|       |   |   `-- Exception.php
|       |   `-- Chain.php
|       |-- Exception.php
|       |-- Loader
|       |   `-- Exception.php
|       |-- Loader.php
|       `-- Options.php

 

You keep all your libraries (framework includes) within a defined library directory which is placed on your include path.

 

set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/library');

 

Within library you create another directory named after your library. eg; PEAR, Zend (yes, the Zend framework uses the same approach) or Mylib.

 

Now, note the location of the EventAbstract file. Within this file, the EventAbstract class would be named....

 

abstract class LibName_Event_Chain_EventAbstract

 

Now, with all that in place you can write an autoload function that can easily locate these classes as long as you stick to the convention.

 

set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/library');

function __autoload_HTTP_Client($class_name) {
  $class_name = str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php';
  require_once($HC);
}

spl_autoload_register('__autoload_HTTP_Client');

 

Does that help?

Link to comment
Share on other sites

Actually, I should re-iterate over what the __autoload_HTTP_Client function is now doing to $class_name. Its simply replacing _ with / (on linux) or \ (on windows).

 

So if you call....

 

$foo = new Libname_Event_Chain;

 

__autoload_HTTP_Client receives 'Libname_Event_Chain' changes it to 'Libname/Event/Chain.php' and then includes it. Because the library directory is on your include path the file gets found.

Link to comment
Share on other sites

Also, don't use require, or at least check if file_exists before calling it. Remember that require will result in fatal error when the file cannot be found, which will stop execution of your script and will not give othe autoloading functions (if any) to try to load your class.

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.