Check out Scrivo

Do you want to try out Scrivo? Then here's a demo for you that does not just get your feet wet but lets you plunge right in.

Contact us

For more information, please contact us. We're happy to help you out!

Next Sep 10 Previous

Item 547194

Which function is called by PHP when you try to use an undefined class in your code. Choose one.

A: spl_autoload_register()
B: load_class()
C: spl_autoload()
D: __autoload()
E: None, a fatal error is raised and the script exits.

Answer

When PHP encounters undefined classes it tries to call __autoload() with the class name as its argument. You need to implement this function yourself if you want to take advantage of this PHP feature.

Actually it could have been any function you like if you had registered it using spl_autoload_register() first. By using this function you can stack a number of autoload functions on top of each other, which is very useful if you are using different class libraries. If you decide to use spl_autoload_register(), which is recommended BTW, PHP will not call __autoload() anymore unless you register it with spl_autoload_register().

The function spl_autoload() is a bit tricky. It is called when spl_autoload_register() was called without parameters. In the absence of a function to register this function will be used as autoload function. It has it's own algorithm for mapping class names to files using the PHP include path and the lower case class name.

load_class() is just a made up function name. It is not used by PHP for auto loading of classes. Unless, of course, it was registered as an autoload function by spl_autoload_register().

PHP throws a fatal error when class loading fails which happens if __autoload() was not implemented, no autoload function was registered using spl_autoload_register() or no autoload function managed to locate the file in which the class was defined.

Answer A is false, the other four answers are all possible depending if spl_autoload_register() was or wasn't used to register autoload functions or an implementation of __autoload() was or wasn't given. If I had to choose one it would be D.