We know that the use of require_once/include_once in PHP, although convenient, but expensive, according to test data, require_once than require 3-4 times slower, so in PHP development, we should try to use require/ Include
Make a list of the methods I use to avoid require/include.
using __autoload
PHP5 can use __autoload to avoid require, with good words, the code is not even see a few require, is really comfortable ah. The test results show that the new Foo after __autoload is used; than require_once ' foo.php '; New Foo; About 3 times times as fast.
Add: To avoid autoload conflicts, consider using Spl_autoload_register (PHP 5 >= 5.1.2) To change the behavior of the Magic function __autoload.
Use defined to detect whether the load has been loaded
Use defined at the beginning of the code to detect if the corresponding constant is defined, and if so, direct return.
<?php
if (!defined (' _myclass_ '))
Return
Define (' _myclass_ ', 1);
Class MyClass {...}
?>
Test, defined performance is not too good ...
pre-require inspection
Check with class_exists or function_exists to confirm that you have not loaded the shot again, at least 3 times times faster than the require_once. PHP4 can also be used.
Class_exists (' MyClass ') or require ('/path/to/myclass.class.php ');