What is the ABSPATH method for?

0

What is the ABSPATH method?

I'm doing a course and the line of code appeared that I do not know what it means.

define('UP_ABSPATH', ABSPATH . '/views/_uploads' );
    
asked by anonymous 20.12.2017 / 18:50

1 answer

1

ABSPATH is not a method. It is actually a constant created in PHP code.

Some systems / frameworks use these constants as this helps maintain code readability and so on.

Imagine the following situation: You have a project with thousands of lines and you need to include the files in a folder, you would have to do the following.

include "system/file1.php"
include "system/file2.php"
include "system/file3.php"
include "system/file4.php"
include "system/file5.php"
include "system/file6.php"

Now imagine that for some reason you decide to change the name of the system folder, look at the work to do this. Then one of the uses of constants comes in.

That way, you would only need to change a single line to take effect on the rest of the code.

define("ABSPATH", "system");

include ABSPATH . "/file1.php"
include ABSPATH . "/file2.php"
include ABSPATH . "/file3.php"
include ABSPATH . "/file4.php"
include ABSPATH . "/file5.php"
include ABSPATH . "/file6.php"

So if you needed to change the name of the system folder, you would only change the value of the constant.

About doubt with define( 'ABSPATH', dirname(__FILE__) );

__ FILE __ is a native PHP con- testate. It is used to indicate the full path of the file being executed.

Ex: You have two /var/www/html/index.php and /var/www/html/index2.php

When you access the file index.php link , PHP automatically assigns the value / var /www/html/index.php for the constant FILE __

The same thing happens when you visit link . PHP automatically assigns the value to __FILE__ as "/var/www/html/index2.php"

The dirname command captures the name of the directory that the current file is located.

If the executed file is "/var/www/html/index2.php", dirname returns only the name "html"

    
20.12.2017 / 18:58