LoginSignup
0
1

More than 1 year has passed since last update.

【PHP】requier_oneceのパスについて

Posted at

requier_oneceのパスについて

発端

  • ファイルパスは合っているはずなのに読み込めなくて何度もハマったのでまとめる。

原因

PHPは、「実行したファイルのあるディレクトリが常に実行時のカレントディレクトリになる」という規則があるためエラーとなってしまっていたっぽい。

解決策

__DIR__もしくはdirname(__FILE__)を使おう

  1. __DIR__は記入したファイルが所属するディレクトリまでのパスを返してくれる。
test--
    |-layout
    |   |_layout.tpl
    |
    |-module
    |   |_module.php
    |
    |-template_c
module.php
// layout.tplを読み込みたい場合
// __DIR__には ~/test/moduleまでのパスが入っている
requir_onece(__DIR__."/../layout/layout.tpl");

2. dirname(__FILE__)は記入したファイルが所属するディレクトリまでのパスを返してくれる。

module.php
// layout.tplを読み込みたい場合
// dirname(__FILE__)には ~/test/moduleまでのパスが入っている
requir_onece(dirname(__FILE__)."/../layout/layout.tpl");
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1