Page 1 of 1

__FILE__ and inheritance

Posted: Wed Oct 29, 2008 10:28 pm
by aaj
This can't be a new problem, but its one I can't seem to tackle (and neither can google, lol).

I'm trying to find a way to get the filename of a subclass from a function defined in its parent class. I get that __FILE__ isn't going to work directly since it always returns the filename of the file it was used in, but there has to be someway short of redeclaring such a simple function in each and every sub class, right?

Code: Select all

 
//SuperClass.php
class SuperClass
{
 
  public function getFile()
  {
     return __FILE__;
  }
}
 
//SubA.php
class SubA extends SuperClass{}
 
//SubB.php
class SubB extends SuperClass{}
 
assuming this layout, getFile() will always return SuperClass.php, regardless if it is called from SubA or SubB, is there anyway (anyway at all, dirty code or otherwise) to get getFile() to return SubA.php and SubB.php respecticely....short of redefining getFile() in each of the sub classes?

Thanks for your time,
Aaj

Re: __FILE__ and inheritance

Posted: Wed Oct 29, 2008 10:35 pm
by s.dot
Interesting!

Well, this seems a bit dirty... :D

But seeing how your classes are named SuperClass and your file is named SuperClass.php (and the subclasses are named in the same manner), perhaps you could play around with get_class() to get the class name and add .php to it to get the file name. :)

Re: __FILE__ and inheritance

Posted: Wed Oct 29, 2008 11:04 pm
by aaj
Aye, that had crossed my mind, but the folder structure (from the root) is important as well (more important than say the class name for the current problem I'm working on) and all three of those classes would be declared in separate folders. :(

Here is a different example, same concept but to illustrate the need for the folder path

Code: Select all

 
//   /Folder1/SuperClass.php
class SuperClass
{
 
  public function getFolder()
  {
     return dirname(__FILE__);
  }
}
 
//    /Folder2/SetC/SubA.php
class SubA extends SuperClass{}
 
//    /Folder2/SetD/SubB.php
class SubB extends SuperClass{}
 
I am looking to have the getfolder() function to return '/Folder2/SetC' and '/Folder2/SetD' respectively for classes SubA and SubB, not '/Folder1' as would be the case as its currently defined.

Aaj