Why define a constant for the same document and check if it exists in the document itself?

2

I see in many codes that programmers are a constant and in the document itself check if this constant exists (has been defined) in itself. I would like to know why this happens.

When we download the open platform from the phpBB3 platform, open the file install/database_update.php , soon in the first lines we notice this:

if (defined('IN_PHPBB') && defined('IN_INSTALL'))
{
    $updates_to_version = UPDATES_TO_VERSION;
    $debug_from_version = DEBUG_FROM_VERSION;
    $oldest_from_version = OLDEST_FROM_VERSION;

    return;
}

define('IN_PHPBB', true);
define('IN_INSTALL', true);

In almost all the files found to be downloaded in the code of phpBB3 , there is this constant define('IN_PHPBB', true); being defined at the beginning of the code.

This is the main example I can remember to elucidate my doubt ...

Based on this code (and some others I've seen, but I will not have my hand), is there any particular reason for this? Is it some kind of good safety practice?

    
asked by anonymous 20.12.2014 / 10:38

1 answer

1

It is hard to say for sure what third party's intention is if it has no documentation. A precise answer could only be given by who did. I can tell you why I would do something like this ... if I did or because I think I would have it in a code.

Defensive schedule

It may seem strange but if one day parts of the code are separated, who will separate it to realize that it would need to verify existence? It's the same principle of using keys even in single-line blocks. The day you need to raise a line, you do not have to worry.

Extensions

A variation of the first reason. In the case PHPBB is a software known to have a system of extensions that often changes the main source code. So it makes sense to check why an extension might create some problem.

Legacy

It was in two sources and they were merged and it would not have to refactor too much. It was easier to leave like this. This happens especially when several programmers work on the same code. This could have happened even because a source was created by code generator and then merged. It is a less likely chance in the possible.

The programmer is lost

I never rule out this possibility. But I always get more with the first and secondarily with the second one.

    
20.12.2014 / 13:53