How to determine whether a file is included

Source: Internet
Author: User
How can I determine whether a file is included?
If (a. php is not included ){
Include ("a. php ");
}


Reply to discussion (solution)

For example, there is the following code: to get the variable values from config respectively, include is written in the function. after the first function is called, the return value can be normal, but the second function, if you use include_once again, the getfile_patn variable is not defined. I don't want to write the include statement on the outermost side. I use globe to declare variables in each function. my idea is that in every function that wants to retrieve the variables in the config file, retrieve the desired variable and release the resource config file at the end of the function to facilitate code management. you can directly include the variable each time you want it.
$ Pat = aa ();
Echo $ pat, PHP_EOL;
$ Post = bb ();
Echo '11', $ post, PHP_EOL;

Function aa ()
{
Include_once (dirname (_ FILE _). '/config. php ');
$ Patten = $ getfile_patn ["search"] ['pattern']; // $ getfile_patn is a variable in config. php.
Return $ patten;

}

Function bb ()
{
Include_once (dirname (_ FILE _). '/config. php ');
Postfix = $ getfile_patn ["search"] ['postfix'];
Turn $ postfix;



}

Define a constant in each contained file, and then determine whether to define a constant to determine whether the file is included.

Write an include class. if it contains a class property, all operations can be done through this class.

Include_once ("a. php ");
Included but not included

Some people say this function is inefficient because it needs to check whether it has been included.
However, can the check program written in php be faster than the c program?

Include_once ("a. php ");
Included but not included

Some people say this function is inefficient because it needs to check whether it has been included.
However, can the check program written in php be faster than the c program?

Because include_once is used in function aa (), function bb can no longer contain files, and thus cannot use variables in the files. this is the problem to be solved.

In the function.
Include (dirname (_ FILE _). '/config. php ');

However, config. php cannot have functions or class definitions.
If yes, use a condition statement to exclude repeated definitions.

Loading a file like you and retrieving only one value from it is very poor.
This type of environment variable is either declared as a global variable or an instance in Singleton mode.

File loading efficiency is always too low.

Function aa () {include_once (dirname (_ FILE __). '/config. php '); $ patten = $ getfile_patn ["search"] ['pattern']; // $ getfile_patn is config. php variable return $ patten ;}


This kind of function is rare .... as Xu said, if you want to retrieve variables and need to reuse them, use global variables or memcache for processing. this is the correct solution.

Now we are using require_once,

Get_included_files ()? Returns the array of the names of include and require files.

Http://www.php.net/manual/zh/function.get-included-files.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.