Re: [PHP] Which file called the function?

2007-12-31 Thread Nathan Nobbe
On Dec 20, 2007 7:06 PM, Robert Cummings <[EMAIL PROTECTED]> wrote:

> On Thu, 2007-12-20 at 19:54 +, George Pitcher wrote:
> > > On Thu, December 20, 2007 8:37 am, Christoph Boget wrote:
> > > > Let's say I have the following 3 files
> > > >
> > > > global.php
> > > >  > > >   function myFunc() { echo __FILE__; }
> > > > ?>
> > > >
> > > > one.php
> > > >  > > >   include( 'global.php' );
> > > >   echo 'You are in file: ';
> > > >   myFunc();
> > > > ?>
> > > >
> > > > two.php
> > > >  > > >   include( 'global.php' );
> > > >   echo 'You are in file: ';
> > > >   myFunc();
> > > > ?>
> > > >
> > > > In each case, what is echoed out for __FILE__ is global.php.  Apart
> > > > from
> > > > analyzing the debug_backtrace array, is there any way that myFunc()
> > > > would
> > > > display "one.php" and "two.php" respectively?
> > >
> > > $_SERVER['PHP_SELF'] and other bits in $_SERVER have the "main" PHP
> > > filename in them.
> > >
> > > __FILE__ will always be exactly the file that it's in.
> > >
> > > In between, I think you are stuck with the debug_backtrace.
> > >
> > > NOTE:
> > > If it's for error logging or error reporting, note that trigger_error
> > > automatically passes in the file/line to the error handler.
> >
> > I'm far from being an expert, and often see more efficient ways of doing
> > things (more efficient tham my own methods). Howevr, I've just been
> dealing
> > with a similar problem.
> >
> > I have a functions file with over 400 functions (and that may be too
> many -
> > but it works). I've been adding some error reporting in to my PEAR::DB
> > queries, but wanted to know which page triggered the function. I went
> > theough my php pages and gane each one a $pg variable at the start
> > containing the bare name of the file, eg 'home' rather than 'home.php'.
> I
> > then went to each function containing either a DB query (ore one which
> calls
> > another DB-related function. I did a global search and replace for the
> > function name 'func(' replaced by 'func($pg,'. I use Dreamweaver so this
> was
> > fairly straightforward.
> >
> > It worked for me, but now someone is about to show me a quicker, cleaner
> > way, aren't they?
>
> Yes, I am.
>
> function checkpoint()
>{
>$trace  = debug_backtrace();
>
>$caller = isset( $trace[0] ) ? $trace[0] : array();
>$owner  = isset( $trace[1] ) ? $trace[1] : array();
>
>$file
>= isset( $caller['file'] )
>? $caller['file']
>: null;
>
>$class
>= isset( $owner['class'] )
>? $owner['class']
>: null;
>
>$function
>= isset( $owner['function'] )
>? $owner['function']
>: null;
>
>$line
>= isset( $caller['line'] )
>? $caller['line']
>: null;
>
>$type
>= isset( $owner['type'] )
>? $owner['type']
>: null;
>
>list( $timeSub, $time ) = explode( ' ', microtime() );
>$timeSub = ereg_replace( '^[^.]\.', '', $timeSub );
>$timeSub = substr( str_pad( $timeSub, 6, '0' ), 0, 6 );
>
>echo 'Checkpoint '
>.'['.date( 'Y-m-d H:i:s', $time ).'.'.$timeSub.']: '
>.($file === null
>? ''
>: $file.' ')
>.($line === null
>? ''
>: sprintf( '{%05d} ', $line ))
>.($class === null
>? ''
>: $class.$type)
>.($function === null
>? ''
>: $function.'()')
>.$this->nl;
>}
>
> ?>
>

incidentally, i was looking through Pear::Log today;
i stumbled upon this and thought id share:

/**
 * Using debug_backtrace(), returns the file, line, and enclosing
function
 * name of the source code context from which log() was invoked.
 *
 * @param   int $depth  The initial number of frames we should step
 *  back into the trace.
 *
 * @return  array   Array containing three strings: the filename, the
line,
 *  and the function name from which log() was called.
 *
 * @access  private
 * @since   Log 1.9.4
 */
function _getBacktraceVars($depth)
{
/* Start by generating a backtrace from the current call (here). */
$backtrace = debug_backtrace();

/*
 * If we were ultimately invoked by the composite handler, we need
to
 * increase our depth one additional level to compensate.
 */
if (strcasecmp(@$backtrace[$depth+1]['class'], 'Log_composite') ==
0) {
$depth++;
}

/*
 * We're interested in the frame which invoked the log() function,
so
 * we need to walk back some number of frames into the backtrace.
The
 * $depth parameter tells us where to start looking.   We go one
step
 * further back to find the name of the e

RE: [PHP] Which file called the function?

2007-12-31 Thread Richard Lynch
On Thu, December 20, 2007 1:54 pm, George Pitcher wrote:
> I have a functions file with over 400 functions (and that may be too
> many -
> but it works). I've been adding some error reporting in to my PEAR::DB
> queries, but wanted to know which page triggered the function. I went
> theough my php pages and gane each one a $pg variable at the start
> containing the bare name of the file, eg 'home' rather than
> 'home.php'. I
> then went to each function containing either a DB query (ore one which
> calls
> another DB-related function. I did a global search and replace for the
> function name 'func(' replaced by 'func($pg,'. I use Dreamweaver so
> this was
> fairly straightforward.
>
> It worked for me, but now someone is about to show me a quicker,
> cleaner
> way, aren't they?

FIRST LINES OF CODE:


Anywhere else in your code:
[examples]

$connection = mysql_connect(...) or trigger_error("No DB access.",
E_USER_ERROR);


-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Which file called the function?

2007-12-21 Thread Cesar D. Rodas
__FILE__ == "global.php"

On 20/12/2007, Christoph Boget <[EMAIL PROTECTED]> wrote:
>
> Let's say I have the following 3 files
>
> global.php
>function myFunc() { echo __FILE__; }
> ?>
>
> one.php
>include( 'global.php' );
>   echo 'You are in file: ';
>   myFunc();
> ?>
>
> two.php
>include( 'global.php' );
>   echo 'You are in file: ';
>   myFunc();
> ?>
>
> In each case, what is echoed out for __FILE__ is global.php.  Apart from
> analyzing the debug_backtrace array, is there any way that myFunc() would
> display "one.php" and "two.php" respectively?
>
> thnx,
> Christoph
>



-- 
Best Regards

Cesar D. Rodas
http://www.cesarodas.com
http://www.thyphp.com
http://www.phpajax.org
Phone: +595-961-974165


RE: [PHP] Which file called the function?

2007-12-20 Thread Shelley Shyan
Richard is right.

If you want to get where __FILE__ is exactly in, __FILE__ is the option.
Else, use $_SERVER['PHP_SELF']. (And this should be what you expected)


Regards,
Shelley


-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Christoph Boget
Sent: Thursday, December 20, 2007 10:37 PM
To: PHP General
Subject: [PHP] Which file called the function?

Let's say I have the following 3 files

global.php


one.php


two.php


In each case, what is echoed out for __FILE__ is global.php.  Apart from 
analyzing the debug_backtrace array, is there any way that myFunc() would 
display "one.php" and "two.php" respectively?

thnx,
Christoph

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Which file called the function?

2007-12-20 Thread Robert Cummings
On Thu, 2007-12-20 at 19:54 +, George Pitcher wrote:
> > On Thu, December 20, 2007 8:37 am, Christoph Boget wrote:
> > > Let's say I have the following 3 files
> > >
> > > global.php
> > >  > >   function myFunc() { echo __FILE__; }
> > > ?>
> > >
> > > one.php
> > >  > >   include( 'global.php' );
> > >   echo 'You are in file: ';
> > >   myFunc();
> > > ?>
> > >
> > > two.php
> > >  > >   include( 'global.php' );
> > >   echo 'You are in file: ';
> > >   myFunc();
> > > ?>
> > >
> > > In each case, what is echoed out for __FILE__ is global.php.  Apart
> > > from
> > > analyzing the debug_backtrace array, is there any way that myFunc()
> > > would
> > > display "one.php" and "two.php" respectively?
> >
> > $_SERVER['PHP_SELF'] and other bits in $_SERVER have the "main" PHP
> > filename in them.
> >
> > __FILE__ will always be exactly the file that it's in.
> >
> > In between, I think you are stuck with the debug_backtrace.
> >
> > NOTE:
> > If it's for error logging or error reporting, note that trigger_error
> > automatically passes in the file/line to the error handler.
> 
> I'm far from being an expert, and often see more efficient ways of doing
> things (more efficient tham my own methods). Howevr, I've just been dealing
> with a similar problem.
> 
> I have a functions file with over 400 functions (and that may be too many -
> but it works). I've been adding some error reporting in to my PEAR::DB
> queries, but wanted to know which page triggered the function. I went
> theough my php pages and gane each one a $pg variable at the start
> containing the bare name of the file, eg 'home' rather than 'home.php'. I
> then went to each function containing either a DB query (ore one which calls
> another DB-related function. I did a global search and replace for the
> function name 'func(' replaced by 'func($pg,'. I use Dreamweaver so this was
> fairly straightforward.
> 
> It worked for me, but now someone is about to show me a quicker, cleaner
> way, aren't they?

Yes, I am.

nl;
}

?>

I often use the above function when debugging code. It prints detailed
information about where it was called. I have something similar in my
error handler but it's not as concise for error related reasons.

It doesn't strike me as an issue that it's a heavy function since it's
used in a debugging context or when an error is present. In which case,
it shouldn't be called in production at least not often :)

Cheers,
Rob.
-- 
...
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Which file called the function?

2007-12-20 Thread George Pitcher
> On Thu, December 20, 2007 8:37 am, Christoph Boget wrote:
> > Let's say I have the following 3 files
> >
> > global.php
> >  >   function myFunc() { echo __FILE__; }
> > ?>
> >
> > one.php
> >  >   include( 'global.php' );
> >   echo 'You are in file: ';
> >   myFunc();
> > ?>
> >
> > two.php
> >  >   include( 'global.php' );
> >   echo 'You are in file: ';
> >   myFunc();
> > ?>
> >
> > In each case, what is echoed out for __FILE__ is global.php.  Apart
> > from
> > analyzing the debug_backtrace array, is there any way that myFunc()
> > would
> > display "one.php" and "two.php" respectively?
>
> $_SERVER['PHP_SELF'] and other bits in $_SERVER have the "main" PHP
> filename in them.
>
> __FILE__ will always be exactly the file that it's in.
>
> In between, I think you are stuck with the debug_backtrace.
>
> NOTE:
> If it's for error logging or error reporting, note that trigger_error
> automatically passes in the file/line to the error handler.

I'm far from being an expert, and often see more efficient ways of doing
things (more efficient tham my own methods). Howevr, I've just been dealing
with a similar problem.

I have a functions file with over 400 functions (and that may be too many -
but it works). I've been adding some error reporting in to my PEAR::DB
queries, but wanted to know which page triggered the function. I went
theough my php pages and gane each one a $pg variable at the start
containing the bare name of the file, eg 'home' rather than 'home.php'. I
then went to each function containing either a DB query (ore one which calls
another DB-related function. I did a global search and replace for the
function name 'func(' replaced by 'func($pg,'. I use Dreamweaver so this was
fairly straightforward.

It worked for me, but now someone is about to show me a quicker, cleaner
way, aren't they?

Cheers, and seasonal greetings from Oxford/Edinburgh

George

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Which file called the function?

2007-12-20 Thread Richard Lynch


On Thu, December 20, 2007 8:37 am, Christoph Boget wrote:
> Let's say I have the following 3 files
>
> global.php
>function myFunc() { echo __FILE__; }
> ?>
>
> one.php
>include( 'global.php' );
>   echo 'You are in file: ';
>   myFunc();
> ?>
>
> two.php
>include( 'global.php' );
>   echo 'You are in file: ';
>   myFunc();
> ?>
>
> In each case, what is echoed out for __FILE__ is global.php.  Apart
> from
> analyzing the debug_backtrace array, is there any way that myFunc()
> would
> display "one.php" and "two.php" respectively?

$_SERVER['PHP_SELF'] and other bits in $_SERVER have the "main" PHP
filename in them.

__FILE__ will always be exactly the file that it's in.

In between, I think you are stuck with the debug_backtrace.

NOTE:
If it's for error logging or error reporting, note that trigger_error
automatically passes in the file/line to the error handler.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Which file called the function?

2007-12-20 Thread Daniel Brown
On Dec 20, 2007 9:47 AM, Christoph Boget <[EMAIL PROTECTED]> wrote:
[snip!]
> I need it primarily for debugging purposes.  I've got a single function
> that's called by several files.  And instead of modifying all the files
> calling the function to add logging, it would be nice to just modify the
> function, adding logging only to it to also include what script called it.
>
>
> I know I can get the information from debug_backtrace but I figured there
> might be a better/easier way.

You probably want http://www.xdebug.org/.

-- 
Daniel P. Brown
[Phone Numbers Go Here!]
[They're Hidden From View!]

If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Which file called the function?

2007-12-20 Thread Jochem Maas
Christoph Boget schreef:
>> I believe __FILE__ is resolved at compile time, not run-time which  
>> means what you're seeing is expected behavior. I'm not sure how you'd  
>> get the name of the file that a function call was made from.
>> Could you explain why you need this information in your application,  
>> and perhaps someone might offer an alternative solution?
> 
> I'm not saying it's not expected behavior.  In fact, that exactly what I
> would expect based on what the docs say.  I'm just asking if there is
> another way to get the script file name that's calling the function.
> 
> I need it primarily for debugging purposes.  I've got a single function
> that's called by several files.  And instead of modifying all the files
> calling the function to add logging, it would be nice to just modify the
> function, adding logging only to it to also include what script called it.
>  
> 
> I know I can get the information from debug_backtrace but I figured there
> might be a better/easier way.

nope, debug_backtrace() is the way to go.

> 
> thnx,
> Christoph
> 
> 
>   
> 
> Looking for last minute shopping deals?  
> Find them fast with Yahoo! Search.  
> http://tools.search.yahoo.com/newsearch/category.php?category=shopping
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Which file called the function?

2007-12-20 Thread Stut

Christoph Boget wrote:
I believe __FILE__ is resolved at compile time, not run-time which  
means what you're seeing is expected behavior. I'm not sure how you'd  
get the name of the file that a function call was made from.
Could you explain why you need this information in your application,  
and perhaps someone might offer an alternative solution?


I'm not saying it's not expected behavior.  In fact, that exactly what I
would expect based on what the docs say.  I'm just asking if there is
another way to get the script file name that's calling the function.

I need it primarily for debugging purposes.  I've got a single function
that's called by several files.  And instead of modifying all the files
calling the function to add logging, it would be nice to just modify the
function, adding logging only to it to also include what script called it.
 


I know I can get the information from debug_backtrace but I figured there
might be a better/easier way.


There isn't. The backtrace is the only thing that can get the call stack.

-Stut

--
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Which file called the function?

2007-12-20 Thread Christoph Boget
> I believe __FILE__ is resolved at compile time, not run-time which  
> means what you're seeing is expected behavior. I'm not sure how you'd  
> get the name of the file that a function call was made from.
> Could you explain why you need this information in your application,  
> and perhaps someone might offer an alternative solution?

I'm not saying it's not expected behavior.  In fact, that exactly what I
would expect based on what the docs say.  I'm just asking if there is
another way to get the script file name that's calling the function.

I need it primarily for debugging purposes.  I've got a single function
that's called by several files.  And instead of modifying all the files
calling the function to add logging, it would be nice to just modify the
function, adding logging only to it to also include what script called it.
 

I know I can get the information from debug_backtrace but I figured there
might be a better/easier way.

thnx,
Christoph


  

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
http://tools.search.yahoo.com/newsearch/category.php?category=shopping

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Which file called the function?

2007-12-20 Thread Nilesh Ashra

On 20 Dec 2007, at 14:37, Christoph Boget wrote:


Let's say I have the following 3 files

global.php


one.php


two.php


In each case, what is echoed out for __FILE__ is global.php.  Apart  
from
analyzing the debug_backtrace array, is there any way that myFunc()  
would

display "one.php" and "two.php" respectively?

thnx,
Christoph


Hi Christoph,

I believe __FILE__ is resolved at compile time, not run-time which  
means what you're seeing is expected behavior. I'm not sure how you'd  
get the name of the file that a function call was made from.


Could you explain why you need this information in your application,  
and perhaps someone might offer an alternative solution?


Regards,

Nilesh

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php