PHP file contains summary include require namespace AutoLoad spl_autoload_register Read file path

Source: Internet
Author: User
Tags autoload

Summarize:

1. Include or require contains other files used./or. /, the current path and the previous layer path, depending on the path of the script being run, will have the following problem.

When you write PHP programs, you often use include or require to include other files, but the files contain more files, resulting in a path problem.

The following directory:

<web> (website root directory)
├<a> folder
││
│└1.php
├<b> folder
││
│└2.php
└index.php

Now index.php in the root directory to include 1.php files in the A folder, you can use the include "./a/1.php"

While the 1.php in the 1 folder contains the 2.php in the B folder, the inclusion "is written in 1.php. /b/2.php "Can

But you know, When the index.php contains 1.php, the compilation is carried out in the index.php, that is, index.php contains the files contained in relative to the index.php, then 1.php is included in the index.php, then the relative to index.php to find Looking for the 2.php. And the above said, 1.php is written in include ". /b/2.php ", the compiled file is now relative to the site root directory (that is, relative to index.php),". /"means returning to the top-level directory and finding it, then how can it be found?

On the internet has also looked for some methods, the best way is to use absolute path method is more appropriate. You can define a single-entry file, include the file to be included, define a constant define ("__root__", DirName (__file__)), then in the process of writing the following file, only need to use absolute mode, plus __root__ on the line.

2. Include or require contains other files that are not used. /include the file name directly, depending on the path where the include files are located, such as:

<web> (website root directory)
├<a> folder
││
│└1.php

││
│└a.php

├<b> folder
││
│└2.php
└index.php

When index.php contains 1.php, the compilation is carried out in the index.php, that is, index.php contains the files contained in relative to the index.php, then 1.php is included in the index.php, if 1.php contains a.php use Require "a.php", there is no problem. using require "./a.php", there is a problem, at this time./represents the path where the index.php is located.

3. The function-loading principle of a namespace is also a containing file, but it is loaded only when it is used, and spl_autoload_register can be used to register the loading mechanism of the namespace.

4. The path of the file and the include and require are different from the file's./filename.ext or. /filename.ext or filename.ext are referenced by the directory in which the script is run (that is, the directory that is obtained by GETCWD ()), so the file operation is recommended with a relative path __file__.

$filename = ' file1.txt ';
$fp = @fopen ($filename, ' w ');

Fwrite ($fp, "\ r \ n");

Fclose ($FP);

$filename = '. /file2.txt ';

PHP file contains summary include require namespace AutoLoad spl_autoload_register Read file path

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.