inheritance - PHP __DIR__ evaluated runtime (late binding)? -
is somehow possible location of php file, evaluated @ runtime? seeking similar magic constant __dir__
, evaluated @ runtime, late binding. similar difference self
, static
:
__dir__ ~ self ??? ~ static
my goal defining method in abstract class, using __dir__
evaluated respectively each heir class. example:
abstract class parent { protected function getdir() { // return __dir__; // not work return <<i need this>>; // } } class heir extends parent { public function dosomething() { $heirdirectory = $this->getdir(); dostuff($heirdirectory); } }
obviously, problem occurs if parent
, heir
in different directories. please, take account. also, defining getdir
on , on again in various heir classes not seem , option, that's why have inheritance...
you can add following code in getdir method of parent class
$reflector = new reflectionclass(get_class($this)); $filename = $reflector->getfilename(); return dirname($filename);
your parent class this
abstract class parent { protected function getdir() { $reflector = new reflectionclass(get_class($this)); $filename = $reflector->getfilename(); return dirname($filename); } }
hoping help.
Comments
Post a Comment