使用php查找扩展类所在的目录

使用php查找扩展类所在的目录,php,Php,假设我们有一个目录 script.php是一个从index.php扩展控制器类的类 application/helloworld/script.php index.php 有没有办法从index.php(controller)返回文件夹helloworld 编辑* script.php class Helloworld extends Controller { function __construct() { echo 'helloworld'; }

假设我们有一个目录

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

application/helloworld/script.php
index.php
有没有办法从index.php(controller)返回文件夹
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();
我会帮你的

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

explode( '/', dirname( __FILE__ ), 1 )
如果您有任何路径,将获得它的最后一部分。把最后一部分切掉。
\uuuu FILE\uuu
常量包含当前文件的路径。所以像
script.php
中的
basename(dirname(\uuuu FILE\uuuu))
应该可以

在PHP5.3及更高版本中,可以将其缩短为
basename(\uuuu DIR\uuuu)

如果您想从
index.php
而不是
script.php
执行此操作,您可以在对象上找到定义它的位置:

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

你能说得更具体些吗?index.php是如何开始接近该文件夹的?script.php是如何发挥作用的?很抱歉,缺少信息。你没有抓住问题的重点
\uuuu FILE\uuuu
获取父类的文件名,而不是子类的文件名。
getcwd()
将获取执行文件的位置(当前工作目录),而不是实例化对象文件所在的位置。例如:
/home/user/index.php
->
包含/home/user/lib/file.php
如果file.php有
echo getcwd()
它将输出
/home/user
,而不是
/home/user/lib