Questions about require in PHP

Source: Internet
Author: User
Question about require in PHP 1. how does one use require in PHP to contain all files in a folder? Require ("./folder/*") is not feasible. isn't it as flexible and convenient as include in Java in PHP? Cannot I recognize the name in regular expressions? Pattern recognition? 2. Do not use require for the same class twice? File & nbsp; nam questions about require in PHP
1. how does require in PHP contain all files in a folder?
Require ("./folder/*") is not feasible. isn't it as flexible and convenient as include in Java in PHP?
Cannot I recognize the name in regular expressions? Pattern recognition?

2. Do not use require for the same class twice?
File name: A_Dao.class.php
Function: contain basic operations of A. class
A_Dao.class.php
Require ("DBConnection. class. php ");
...
...
...
?>

Another file: B _Dao.class.php
Function: contain basic operations of B. class
B _Dao.class.php
Require ("DBConnection. class. php ");
...
...
...
?>

A and B. class are entities as abstracted from conceptions.

ServiceA. class. php
Require ("./dao/A_Dao.class.php ");
Require ("./dao/B _dao.class.php ");
...
...
...
?>
Run ServiceA. class. php
"Fatal error: Cannot redeclare class DBConnection in C: \ Program Files \ Apache Software Foundation \ Apache2.2 \ htdocs \ application \ dao \ DBConnection. class. php on line 3"

What's wrong?
Why can't I repeat the same require (DBConnection. class. php )?
Will the compiler not always perform dynamic processing (like Java )?
------ Solution --------------------
Why does it require?
Require inserts all the target files into the current file. If * is allowed, all files in the directory will be inserted, so that the file will be super large.

Second question:
Require_once

C/C ++ does not allow repeated include of the same file.
------ Solution --------------------
Php's include is similar to the C language, and every execution page will include,

If this option is supported, I think it will seriously affect the efficiency.

Unlike the java import java. io. *; after compilation, only the library is imported for use and the. class size is not affected.
------ Solution --------------------
Require (". /folder/* ") will increase the burden on the server, because if the Web server loads all the files under the folder, the server will not be able to withstand each request.

Php can contain only once. you can use require_once to avoid repeated require.
------ Solution --------------------
You can use Autoload
------ Solution --------------------
Require_once ()
------ Solution --------------------
1. in comparison, Java can be said to be a "symbol-level" inclusion. in PHP, no matter include/include_once/require/require_once, it is a "file-level" inclusion. therefore, PHP does not support wildcards. If you want to "include all program files in a folder", you can use scandir () or similar methods to traverse and include files. As long as it is "needed", there is no efficiency or inefficiency.

2. use require_once. What the brothers on the first and third floors say is inaccurate. PHP/C ++ can be contained multiple times. Errors are often reported during repeated inclusion in PHP, because the function and class cannot be "repeatedly defined.


--------------------------------
[Img = Beijing ...... When can I get these ......
--------------------------------
Based on the plug-in extension function provided by the CSDN forum, I have made a signing tool and shared it with you. we welcome Technical Exchange :)

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.