Forum Moderators: coopster

Message Too Old, No Replies

Examples of Autoloading Objects in PHP.

         

duckxtales

11:55 pm on Feb 12, 2007 (gmt 0)

10+ Year Member



I'm a bit confused with autoloading objects.

This is only for PHP5, correct?

All autoloading functions start with "__" correct?

And can someone give me a easy to understand example of an autoloading object? I googled, but I still have questions about it.

eelixduppy

12:11 am on Feb 13, 2007 (gmt 0)




This is only for PHP5, correct?

Yes.

Here's an example, taken from the documentation [us3.php.net]:


<?php
#this autoload function gets called if the script cannot find the class definition
function __autoload($class_name) {
require_once $class_name . '.php'; #includes the class
}

$obj = new MyClass1();
$obj2 = new MyClass2();
?>

duckxtales

6:59 am on Feb 13, 2007 (gmt 0)

10+ Year Member



the question is does any function just run automatically when it has __ in front of the function name when the class is declared?

eelixduppy

11:56 am on Feb 13, 2007 (gmt 0)



I'm pretty sure only
__autoload
will work, and no other function (unless you call that function within the autoload one).