PHP - __autoload Hints

PHP5 has a great feature for us lazy OOP people. With __autoload() you can skip over some of the hell of including every class file. But wait. Don't use __autoload()! Instead, so that your script plays nice with others you should use spl_autoload_register('my_overload_function'); This allows multiple autoload functions so that yours doesn't hog __autoload.

Now, onto what tripped me up for a few minutes. I placed a class in the session and then registered the autoload function in my cart include file. Instead of having everything magically work, I got this error:

Fatal error: Cart::calculateCartTotal() [function.Cart-calculateCartTotal]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "CustomCartItem" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in /home/www/demo/includes/cart.inc.php on line 71

Sucks. So what's going on here? Taking a quick dump of the class I saw the following:

print_rr Invoked from: /home/www/demo/includes/cart.inc.php - 70
__PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => CustomCartItem
[ItemType] => CI
...

Looks like autoload didn't feel like working, right? Eh, not quite. I session_start()'d before the cart.inc.php file was loaded. This is where I stuck my autoload. Oops. So, make sure to have your autoload functions loaded in and registered before your session_start.