[Solar-talk] __autoload()

Paul M Jones pmjones at solarphp.com
Wed Jul 12 09:14:15 PDT 2006


Hi Rodrigo,

> However, Solar makes
> __autoload() impossible, at least using it with Solar::loadClass()
> inside.
>
> The problem resides in both Solar::loadClass() and Solar::run(), which
> throw exceptions if the class is not loaded or the file is not found.

That's true; using Solar::loadClass() inside __autoload() won't work  
because of the exception usage. [*]

There are some folks who love the idea of __autoload(), and there are  
others who can't stand it.  I fall kind of in the middle:  for  
specific projects, it's useful, but I don't think a framework ought  
to implement it, because there are too many assumptions about how the  
target project is organized.

If a project is built entirely in the PEAR/Solar style, then  
__autoload() is not a problem.  However, some projects use 3rd-party  
libs that are not PEAR/Solar-ish, and that *will* cause problems.

As a result, I think it's best if projects develop their own  
__autoload().  It's trivial to re-implement the Solar::loadClass()  
file-finding logic inside a project-specific __autoload(), and then  
you can combine that with whatever file-finding logic you might need  
for 3rd-party libs.

Having said all that, perhaps it would be possible to provide a call  
that spl_autoload() can use, instead of the standard PHP __autoload().

     http://php.net/spl_autoload

> PS: I'm more and more amazed with Solar. ;)

You're very kind to say; thank you.  :-)

----

[*] The main reason for the exceptions is so that you can get a stack  
trace to see where the loading was attempted from.  With require(),  
you get an error that the require failed, but it doesn't tell you  
what file/line/etc; with loadClass(), when it fails, the stack trace  
tells you exactly where the failure originates.




--

Paul M. Jones  <http://paul-m-jones.com>

Solar: Simple Object Library and Application Repository
for PHP5.   <http://solarphp.com>

Savant: The simple, elegant, and powerful solution for
templates in PHP.   <http://phpsavant.com>




More information about the solar-talk mailing list