


After testing, require_once is a syntax structure with low performance. Of course, this low performance is relative to require. This article explains the require method currently used in our project and proves its efficiency through experimental code. At the same time , describe the problems we encountered during use, and avoid others stumbling on the same stone.
- require: Introduce a file and compile and import it at runtime.
- require_once: The function is equivalent to require, except that when this file is referenced, it will no longer be compiled and imported.
The above is the difference between the two. It can be seen that the only difference between the two is that require_once has a mechanism to determine whether it has been referenced. Through Internet searches, you can see a lot of data about the performance of require_once being much lower than require. This experiment will not be done here.
The approach in our project is: Define a global variable at the beginning of each file. When require, use isset($xxxxxx) or require 'xxxxx.php';
What are the disadvantages of this approach?
When a global variable is defined in $xxx, if the file is required within the function, the variable will be parsed as a local variable of the function instead of global. Therefore, isset($xxx) or require inside the function The syntactic structure of 'xxx.php' will be invalid, and the results will be unexpected, such as class redefinition, method redefinition, etc.
Learning from the past, so to define global variables, please use $GLOBALS['xxx']. When requiring, use isset($GLOBALS['xxx']) or require 'xxx.php';. Using GLOBALS will be better than Direct definition is a little slower, but it's much better than being wrong.
Since our previous global variables were directly defined, during the discussion with colleagues today, another way of writing came to mind:
The defined position is still defined directly using the $xxx method, and modified in the require method (the global variables defined in the file header are related to the file name).
function ud_require($xxx) { global $$xxx; isset($$xxx) or require $xxx . '.php'; }
This method uses dynamic variables. Compared with the direct GLOBALS method, it has two significant disadvantages:
- Performance, due to the introduction of dynamic variables, is about 2 times slower than the GLOBALS method.
- The indirect reference problem cannot be solved because we cannot predict the file name that is indirectly referenced, and we cannot use global to declare the marked global variables defined in the indirectly referenced files.
Okay, here is my test of require and require_once in GLOBALS method:
require_requireonce.php
<?php function test1($filename) { //pathinfo($filename); isset($filename) or require $filename; } function test2() { require_once 'require_requireonce_requireonce.php'; } $start = microtime(true); while($i ++ < 1000000) isset($GLOBALS['require_requireonce_require.php']) or require 'require_requireonce_require.php'; $end = microtime(true); echo "不使用方法的isset or require方式: " . ($end - $start) . "<br />/n"; $start = microtime(true); while($j ++ < 1000000) test1('require_requireonce_require.php'); $end = microtime(true); echo "使用方法的isset or require方式: " . ($end - $start) . "<br />/n"; $start = microtime(true); while($k ++ < 1000000) test2(); $end = microtime(true); echo "require_once方式: " . ($end - $start) . "<br />/n"; ?>
require_requireonce_require.php (introduced file used to test require)
<?php $GLOBALS['require_requireonce_require.php'] = 1; class T1 {} ?>
require_requireonce_requireonce.php (introduced file used to test require_once)
<?php class T2 {} ?>
The following are the test results (unit: seconds):
- Isset or require method without using method: 0.22953701019287
- Use isset or require method: 0.23866105079651
- require_once method: 2.3119640350342
It can be seen that the require speed without a method is slightly faster than that using the method. Both speeds are about 10 times that of require_once.
So, where is the performance loss?
In the test1 method in the require_requireone.php file above, I commented pathinfo($filename), because my original intention was to use the file name without a suffix as a marked global variable name, but when I use After pathinfo, I found that the performance consumption of this method is basically the same as require_once. Therefore, I added a separate call to pathinfo there and did a test. Sure enough, pathinfo was causing trouble. Therefore, I later modified it to the current version and directly used the file name as the variable name. If you are afraid of duplicate file names, you might as well add the path name...
Guess: After adding pathinfo, the performance consumption of require and require_once is basically the same. So can we guess that PHP's internal processing of require_once is based on it? It is said that require_once has been significantly optimized in PHP5.3. However, I used the PHP5.3.5 version during the test, and I can still see the obvious gap with require. Is it just a larger optimization than the previous version? This has not been tested yet....
Try to modify the test1 method as follows: isset($GLOBALS[substr($filename, 0, strlen($filename) - 4)]) or require $filename;
Use manual string interception. Of course, interception is time-consuming, but it is better than the pathinfo version. The test result this time is:
- Isset or require method without using method: 0.21035599708557
- Use isset or require method: 0.92985796928406
- require_once method: 2.3799331188202
When changing require_once to isset or require mode, you need to pay attention to the following aspects:
- Each file header defines a unique tag variable, defined using $GLOBALS['XXX'] = 1;, and it is recommended that the variable name be the file name or a file name with a path (if a separate file Famous party repeats)
- Define a custom require method:
function ud_require_once($filename) { isset($GLOBALS[$filename]) or require $filename; }

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
