1.2.301. Could Use __DIR__

Use __DIR__ constant to access the current file’s parent directory <https://www.php.net/`directory>`_.

Avoid using dirname() on __FILE__. __DIR__ has been introduced in PHP 5.3.0.

<?php

// Better way
$fp = fopen(__DIR__.'/myfile.txt', 'r');

// compatible, but slow way
$fp = fopen(dirname(__FILE__).'/myfile.txt', 'r');

// Since PHP 5.3
assert(dirname(__FILE__) == __DIR__);

?>

See also Magic Constants.

1.2.301.1. Suggestions

  • Use __DIR__ instead of dirname(__FILE__);

1.2.301.2. Specs

Short name

Structures/CouldUseDir

Rulesets

All, Analyze, CE, CI-checks, Suggestions, php-cs-fixable

Exakat since

0.8.4

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Features

magic-constant

Examples

Woocommerce, Piwigo

Available in

Entreprise Edition, Community Edition, Exakat Cloud