开发者

find directory where my extended class is with php

lets say we have a dir

script.php is a class that is extending a controller class from index.php,

application/helloworld/script.php
index.php

is there a way to return the folder helloworld from index.php ( controller ) ?

edit*

script.php

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

index.php

class Controller
{
    function wherethescript(){
        # trying to find folder where helloworld开发者_如何学JAVA.php is in from this class
    }
}

include_once 'application/helloworld/helloworld.php';
$x=new Helloworld;
$x->wherethescript();


If you have any path, basename will get you the last part of it. dirname chops off the last part. The __FILE__ constant contains the path of the current file. So something like basename(dirname(__FILE__)) in script.php should do.

That can be shortened to basename(__DIR__) in PHP 5.3 and up.

If you want to do this from index.php and not script.php, you can reflect on the object to get where it was defined:

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


dirname() and explode() will do that for you.

With dirname() you will get the full path, with the line below only the current dir; getcwd() will also help.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜