找到目录,我的扩展类是与php


find directory where my extended class is with php

假设我们有一个dir

script.php是一个从index.php扩展的控制器类,

application/helloworld/script.php
index.php

是否有办法从index.php(控制器)返回文件夹helloworld ?

编辑*

script.php

class Helloworld extends Controller
{
    function __construct()
    {
        echo 'helloworld';  
    }
}

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();

如果您有任何路径,basename将为您提供它的最后一部分。dirname切掉最后一部分。__FILE__常量包含当前文件的路径。所以像script.php中的basename(dirname(__FILE__))就可以了。

在PHP 5.3或更高版本中可以缩短为basename(__DIR__)

如果你想从index.php而不是script.php中做到这一点,你可以在对象上反射到它被定义的地方:

$helloReflection = new ReflectionClass($this);
echo $helloReflection->getFilename();

dirname()和explosion()将为您完成。

使用dirname()您将获得完整路径,下面的行仅为当前目录;Getcwd()也会有帮助。

explode( '/', dirname( __FILE__ ), 1 )