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();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With