How to avoid using PHP's require

Source: Internet
Author: User

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 ');



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.