What is the ABSPATH method for?

Asked

Viewed 2,591 times

1

What is the method for ABSPATH?

I’m taking a course and the line of code came up that I don’t know what it means.

define('UP_ABSPATH', ABSPATH . '/views/_uploads' );
  • ABSPATH is a constant created by you or your framework. The idea should be to fix the path of something (libs, views or anything else)

  • I understood, but there is another line with the same method. defines( 'ABSPATH', dirname( FILE ) );

1 answer

2


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

Some systems/frameworks use these constants as this helps maintenance, readability of code and etc.

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

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 folder system, look at the work to do this. Therein comes one of the uses of constant.

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 the doubt with define( 'ABSPATH', dirname(__FILE__) );

__FILE__ is a native PHP check. It serves to indicate the full path the file is running.

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

When you access the file index.php http://www.example.com/index.php, PHP automatically assigns the value /var/www/html/index.php to the constant __FILE__

The same occurs when you access http://www.example.com/index2.php. PHP automatically assigns the value to __FILE__ as "/var/www/html/index2.php"

Already the command dirname serves to capture the name of the directory that is located the current file.

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

  • Dear thank you that excellent explanation, I was only in doubt of this other line that appeared in the code define( 'ABSPATH', dirname( FILE ) );

  • @sol1010lua edited my answer. I added a brief explanation about the code define( 'ABSPATH', dirname(__FILE__) );.

  • Very good explanation where helped me and a lot to understand these codes, vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.