Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing the file where included from within a class

Status
Not open for further replies.

JavaSprout

Programmer
Jul 31, 2001
30
BE
I'm a newbie to this PHP, so this question I'm sure will be a very easy one for you!

I am trying to write a class which can be used in multiple places, for example within abc.php and xyz.php. I want within the class to have a reference to the file which it gets include within, i.e. to abc or to xyz. Of course, at class writing time, I do not know what the name of the file including it is going to be. Therefore I need something like $this, but $this refers to the class.

abc.php is:
include("link_bot.php");
link_bot->show_link(1); // I want link abc.php?key=1

xyz.php is:
include("link_bot.php");
link_bot->show_link(1); // I want link xyz.php?key=1

link_bot.php is:
<?
class link_bot
{
function show_link( $key )
{
// Notice the hard coding of the name 'xyz'
print(&quot;<a href=xyz?key=$key>page $key</a>&quot;);
}
}
?>

Thanks for any help!
 
One way to do that is setting a global var named $LASTFILE and setting the name of the current file to it.

Other way is passing the name in paramters to the function.

But there is an easier way to do that, if the file including link_bot is the main file, the one you call in the link. Then, in this case, you have a special var named $PHP_SELF or $_SERVER['PHP_SELF'] (assuming the use of php4.1.x) and in this var you get the name of the script you are running in the moment. If you are using php4.1.x you don't have to declare the $_SERVER['PHP_SELF'] as global. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Stunning!

$PHP_SELF - That's EXACTLY what I was looking for!

Thanks a million.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top