简体   繁体   中英

PHP use of define and __DIR__ explanation

I have been inspecting code, reading questions on StackOverflow, but I simply don't get it, or rather I dont understand the explanations and / or logic behind it.

Consider The Following

If I have a directory structure like this

在此处输入图片说明

Now I want to set the head.php file to be globally accessible throughout the application ( just as an example )

define('Head', __DIR__ .'/views/head.php');

If I do the above, I get the following result:

C:\\xampp\\htdocs\\carRental/views/head.php"

Which is technically what I want,, however, notice the URL contains forward and backslashes?

Can I get access to the head.php file by calling Head anywhere in my directory tree?

Im sorry, Ive been inspecting code and read the manual and questions on here, if anyone could give a rookie a clear explanation it would be greatly appreciated.

UPDATE:

When I try to do the following in landingPage.php I get the following errors

include_once Head;

Notice: Use of undefined constant Head - assumed 'Head' in C:\\xampp\\htdocs\\carRental\\views\\landingPage.php on line 2

Warning: include_once(Head): failed to open stream: No such file or directory in C:\\xampp\\htdocs\\carRental\\views\\landingPage.php on line 2

Warning: include_once(): Failed opening 'Head' for inclusion (include_path='C:\\xampp\\php\\PEAR') in C:\\xampp\\htdocs\\carRental\\views\\landingPage.php on line 2

When you used define('Head', __DIR__ .'/views/head.php'); you have hardcoded the slashes in the definition.

However windows by default uses \\ as the default directory separator so __DIR__ will be using \\ in the path when in Windows (it's ok with using / as an alternative one though so it shouldn't be a problem).

You can do the following if you want them to be consistent:

define('Head', __DIR__ .DIRECTORY_SEPARATOR ."views".DIRECTORY_SEPARATOR ."head.php");

Which is technically what I want,, however, notice the URL contains forward and backslashes?

yes

Can I get access to the head.php file by calling Head anywhere in my directory tree?

no

The mix of foreward and backward slashes is created by your command __DIR__ creates C:\\xampp\\htdocs\\carRental and '/views/head.php' is the string you append.

To be able to use your defined HEAD you would have to load the php file which defines it. another php files does not know what this file does or doesn't do as long as it is not persisted. (which you don't do in the code provided) To load a file and make your definition available use include_once / require_once

__DIR__ will always resolve to the absolute directory of the file using it.

The reason for forward and backslashes. This part:

C:\xampp\htdocs\

Is Windows file path. This part:

carRental/views/head.php

Is the webserver path, ie not Windows.

Your define will hold the correct file path so try to include it now:

include_once Head;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM