__FILE__ and inheritance

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aaj
Forum Newbie
Posts: 4
Joined: Wed Oct 29, 2008 10:20 pm

__FILE__ and inheritance

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: __FILE__ and inheritance

Post 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. :)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
aaj
Forum Newbie
Posts: 4
Joined: Wed Oct 29, 2008 10:20 pm

Re: __FILE__ and inheritance

Post 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
Post Reply