Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find directory where my extended class is with php

Tags:

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.php is in from this class
    }
}

include_once 'application/helloworld/helloworld.php';
$x=new Helloworld;
$x->wherethescript();
like image 948
Adam Ramadhan Avatar asked Jul 30 '11 08:07

Adam Ramadhan


1 Answers

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();
like image 73
deceze Avatar answered Oct 08 '22 00:10

deceze