让我们说我们有一个目录
script.php是一个从index.php扩展控制器类的类,
application/helloworld/script.php
index.php
Run Code Online (Sandbox Code Playgroud)
有没有办法helloworld从index.php(控制器)返回文件夹?
编辑*
script.php的
class Helloworld extends Controller
{
function __construct()
{
echo 'helloworld';
}
}
Run Code Online (Sandbox Code Playgroud)
的index.php
class Controller
{
function wherethescript(){
# trying to find folder where helloworld.php is in from this class
}
}
include_once 'application/helloworld/helloworld.php';
$x=new Helloworld;
$x->wherethescript();
Run Code Online (Sandbox Code Playgroud)
如果您有任何路径,basename将为您提供最后一部分.dirname砍掉最后一部分.该__FILE__常量包含当前文件的路径.因此,像basename(dirname(__FILE__))中script.php应该做的.
这可以缩短到basename(__DIR__)PHP 5.3及更高版本.
如果您想要从而index.php不是这样做script.php,您可以反思对象以获取它的定义:
$helloReflection = new ReflectionClass($this);
echo $helloReflection->getFilename();
Run Code Online (Sandbox Code Playgroud)