php 如何做包括正确的方法
php how to do include right way
您好,我是 运行 WAMP(32 位)服务器
i exec C:\wamp\www\index.php 使用此地址的浏览器 http://localhost:8080/1/
index.php 有以下代码
require_once 'libraries/database/database.php';
database.php 有以下代码
require_once '../misc/traits/singleton.php';
singleton.php 位于此位置
C:\wamp\www\libraries\misc\traits\singleton.php
database.php 位于此位置
C:\wamp\www\libraries\database\database.php
有错误:
( !) 警告:require_once(../misc/traits/singleton.php):无法打开流:C:\wamp\www\libraries\database\database 中没有此类文件或目录。 php 第 3 行
( !) 致命错误:require_once(): 需要打开失败 '../misc/traits/singleton.php' (include_path='.;C:\php\pear') 在 C:\wamp\www\libraries\database\database.php 第 3
行
谢谢
路径源自最初执行的 php 文件,除非您使用 chdir 更改路径。
所以使用'libraries/misc/traits/singleton.php'
如果要进行相对包含,请使用 __DIR__
,这是当前文件的目录。
例如
__DIR__ . '/../misc/traits/singleton.php'
您好,我是 运行 WAMP(32 位)服务器
i exec C:\wamp\www\index.php 使用此地址的浏览器 http://localhost:8080/1/
index.php 有以下代码
require_once 'libraries/database/database.php';
database.php 有以下代码
require_once '../misc/traits/singleton.php';
singleton.php 位于此位置
C:\wamp\www\libraries\misc\traits\singleton.php
database.php 位于此位置
C:\wamp\www\libraries\database\database.php
有错误:
( !) 警告:require_once(../misc/traits/singleton.php):无法打开流:C:\wamp\www\libraries\database\database 中没有此类文件或目录。 php 第 3 行
( !) 致命错误:require_once(): 需要打开失败 '../misc/traits/singleton.php' (include_path='.;C:\php\pear') 在 C:\wamp\www\libraries\database\database.php 第 3
行谢谢
路径源自最初执行的 php 文件,除非您使用 chdir 更改路径。
所以使用'libraries/misc/traits/singleton.php'
如果要进行相对包含,请使用 __DIR__
,这是当前文件的目录。
例如
__DIR__ . '/../misc/traits/singleton.php'