Skip to content

Herman J. Radtke III

Using the PHP 5.3 __DIR__ magic constant

There is a new magic constant in PHP 5.3: DIR. This new constant does not actually do anything new. It replaces the use of dirname(FILE) that is commonly found code using prior PHP versions.

One of the troubles with including files is that the current directory is constantly a moving target. Consider the following structure:

index.php
admin/index.php
includes/
    foo.php
    bar.php

Imagine for a moment that both index.php and admin/index.php include includes/foo.php. If foo.php includes bar.php, the relative directory is either "../" or "../admin/" depending on whether index.php or admin/index.php was executed. The use of the dirname() function along with the FILE magic constant solved this problem for a long time.

The FILE magic constant gives the full path and filename of the current file. The dirname() function gives the directory name of a file path. This was used prior to PHP 5.3 to solve the include problem described above.


In PHP 5.3, this has been super-ceded by the use of the DIR magic constant.


Why the change? Well, it is more succinct and more clear on what is going on. Also, the magic constant is part of the Zend engine, so it is a little more performant as well.