2

Problem

I am using the SPL Class Loader provided by PSR Group. However, I've come to a chicken-egg paradox.

That is, this will autoload classes preventing explicitly including them. But I first need to include/require this class and the code for instantiating the object

$classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine');
$classLoader->register();

Question

What is common solution for including the autoloader code?

In the case of a front-end controller, I could add the code there. But I'm not always using the front-end controller patter. I suppose I could also use PHP's auto_prepend_file. Interested in the communities input.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174

1 Answers1

2

You explicitly require the autoloader on the bootstrap stage (the stage where the application is started up).

All subsequent functions/classes are then autoloaded.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • Fine for an *app*. What if I just have a large codebase, with multiple entry points (e.g. APIs)? – Jason McCreary Nov 29 '12 at 18:08
  • 1
    Are you seriously saying that you *don't* have a bootstrap or init file for your application? Do you have all of your init logic spread across all of your entry points? Fix that. – Charles Nov 30 '12 at 08:24
  • @Charles, no that's not what I'm saying. – Jason McCreary Jan 22 '13 at 01:41
  • While this didn't thoroughly answer *my* question, it is the general approach. If you're not familiar with *"bootstrap"*, read [what is bootstrapping](http://stackoverflow.com/questions/1254542/what-is-bootstrapping). – Jason McCreary Mar 30 '13 at 12:02