## 使用?[spl_autoload_register()](http://php.net/manual/zh/function.spl-autoload-register.php)?來注冊你的自動加載函數。
PHP 提供了若干方式來自動加載包含還未加載的類的文件。 老的方法是使用名為?[`__autoload()`](http://php.net/manual/zh/function.autoload.php)?魔術全局函數。 然而你一次僅能定義一個?`__autoload()`?函數,因此如果你的程序包含一個也使用了?`__autoload()`?函數的庫,就會發生沖突。
處理這個問題的正確方法是唯一地命名你的自動加載函數,然后使用?`spl_autoload_register()`?函數來注冊它。 該函數允許定義多個?`__autoload()`?這樣的函數,因此你不必擔心其他代碼的?`__autoload()`?函數。
## 示例
~~~
<?php
// 首先,定義你的自動載入的函數
function MyAutoload($className){
include_once($className . '.php');
}
// 然后注冊它。
spl_autoload_register('MyAutoload');
// Try it out!
// 因為我們沒包含一個定義有 MyClass 的文件,所以自動加載器會介入并包含 MyClass.php。
// 在本例中,假定在 MyClass.php 文件中定義了 MyClass 類。
$var = new MyClass();
?>
~~~
## 進一步閱讀
* [PHP手冊:spl_autoload_register()](http://php.net/manual/zh/function.spl-autoload-register.php)
* [Stack Overflow: 高效的 PHP 自動加載和命名策略](http://stackoverflow.com/questions/791899/efficient-php-auto-loading-and-naming-strategies)