On Thu, 2007-12-20 at 19:54 +0000, 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.

<?php
    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;
    }

?>

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

Reply via email to