PHP __ DIR__ ve dirname (__FILE__) arasında herhangi bir fark var mı?

1 Cevap

, Bu benim için aynı görünüyor, ama emin değilim

dirname(__FILE__) kullanan pek çok proje var çünkü.

1 Cevap

Their result is exactly the same; böylece, o fark yok.


For example, the two following lines :

var_dump(dirname(__FILE__));
var_dump(__DIR__);

Ikisi aynı çıktı verecektir:

string '/home/squale/developpement/tests/temp' (length=37)


But, there are at least two differences :

  • __DIR__ only exists with PHP >= 5.3
    • dirname(__FILE__) daha yaygın olarak kullanılmaktadır neden olan
  • __DIR__ is evaluated at compile-time, while dirname(__FILE__) means a function-call and is evaluated at execution-time
    • böylece, ilk (or, should be) hızlıdır.


As, as a reference, see the Magic constants section of the manual (quoting) :

__DIR__ : The directory of the file.
If used inside an include, the directory of the included file is returned.
This is equivalent to dirname(__FILE__).
This directory name does not have a trailing slash unless it is the root directory.
(Added in PHP 5.3.0.)