Calling function with the "include file name"

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
markbeadle
Forum Commoner
Posts: 29
Joined: Tue Dec 02, 2003 2:50 am
Location: Aachen, Germany

Calling function with the "include file name"

Post by markbeadle »

Hello Alll,

I seem to remember having read somewhere that there is a facility for calling a function with additional information beforehand which shows which include file the function is held in.

Example:
file xyz.php holds function getHello();

Main file
<?php
include("xyz");

echo(<method>getHello());
?>

Has anyone come across this ? Are they able replace the <method> text ?

I realise the other method could be echo(getHello()); // function in xyz.php

Thanks
Mark
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

First of all, you have some bad things in your code;

Code: Select all

<?php

include("xyz.php"); //You forgot the .php extension

echo "<method>getHello()"; //you forgot " and "

?>
if you want to replace <method> by getHello(), use str_replace
look at the PHP manual under strings for it
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

__FILE__ and __LINE__ might be what you are looking for.

Note that you can't build a single use-anywhere function to wrap that up: they'll always return the custom function location and not the location where you execute the fn.

I use this quite a lot for debugging. First I build a $location var with the above two constants, and then pass it in to a $monitor logging object. Very useful if your scripts are composed of multiple files.
markbeadle
Forum Commoner
Posts: 29
Joined: Tue Dec 02, 2003 2:50 am
Location: Aachen, Germany

Post by markbeadle »

McGruff wrote:__FILE__ and __LINE__ might be what you are looking for.
Thanks McGruff. Very useful to know.
Post Reply