Professional Web Applications Themes

[PHP] Determining where a function is called from.. - PHP Development

Thanks David, I noticed the __LINE__ and __FUNCTION__ magic constants too, but they don't really contain the values I was looking for, as they have the current function and line in it. But what I want my Logging class (I have one too :D) to display is what I am about to illustrate with an example: script: index.php ----------------- 01 $Database->connect('host', 'database', 'username', 'password'); (...) 20 $Database->execute('DELETE news WHERE id IN ()'); -> this would produce an error in my Database class, error in sql syntax script: class.database.inc.php ------------------------------ 2 class Database { 3 4 var $C; # Connection resource ...

  1. #1

    Default RE: [PHP] Determining where a function is called from..

    Thanks David,

    I noticed the __LINE__ and __FUNCTION__ magic constants too, but they don't
    really contain the values I was looking for, as they have the current
    function and line in it. But what I want my Logging class (I have one too
    :D) to display is what I am about to illustrate with an example:

    script: index.php
    -----------------
    01 $Database->connect('host', 'database', 'username', 'password');
    (...)
    20 $Database->execute('DELETE news WHERE id IN ()');

    -> this would produce an error in my Database class, error in sql syntax

    script: class.database.inc.php
    ------------------------------
    2 class Database {
    3
    4 var $C; # Connection resource
    5 var $S; # Settings
    6 var $L; # Log Object
    7 var $R; # Result
    8
    9 function Database($Host, $Database, $Username, $Password) {
    10 global $Log;
    11
    12 $this->S = Array( 'host' => $Host,
    13 'database' => $Database,
    14 'username' => $Username,
    15 'password' => $Password
    16 );
    17
    18 $this->L = $Log;
    19
    20 return true;
    21 }
    (...)
    55 function execute() {
    56 $this->connect(false);
    57
    58 # Prepare
    59 $Query = $this->prepare(func_get_args());
    60 $this->dblog($Query);
    61
    62 # Execute the query
    63 $this->R['resource'] = mysql_query($Query,
    $this->C) or $this->dberror();
    64 $this->R['affectedrows'] =
    mysql_affected_rows($this->C) or $this->dberror();
    65
    66 return $this->R['affectedrows'];
    67 }
    (...)
    171 function dberror() {
    172 if ($String = mysql_error($this->C)) {
    173 $OldType = $this->L->setLogType(L_STDOUT);
    174 $this->L->Error('[database] '.$String);
    175 $this->L->setLogType($OldType);
    176 };
    177 }
    178 };

    I could fairly live with passing __LINE__ and __FUNCTION__ at some places,
    but not in my index.php (seperating front- and backend). What I want
    dberror() to pass to my logging module is something like "You have an error
    in your sql syntax at line 1, called from index.php on line 20". The first
    part being just the mysql error, after the comma from where the errorfull
    query was called ..

    Thanks once more,
    Wouter

    Ps. or am I really wanting too much now?

    -----Oorspronkelijk bericht-----
    Van: David Otton [mailto:phpmailjawbone.freeserve.co.uk]
    Verzonden: vrijdag 15 augustus 2003 16:13
    Aan: Wouter van Vliet
    Onderwerp: Re: [PHP] Determining where a function is called from..


    On Fri, 15 Aug 2003 16:02:24 +0200, you wrote:
    >I've got this problem .. not really a problem but still some thing to my
    >disliking. For a project I wrote some database handling class, no big deal.
    >When a function from this module is called with a query containing an
    error,
    >or some other error occurs inside this class it reports to my that an error
    >occured inside that class. "Line blabla of class.database.inc.php". But
    what
    >I really want to know is on what line the $Database->select() or such was
    >called so that I can effectively trace the error.
    >
    >So, for effective error reporting I would like to know if there are
    >functions or constants containing
    > - the script-name a user-function is called from
    > - the linenumber of this script a user-function is called from
    >
    > - And maybe it would even be doable to get an entire stack-trace of files
    >and lines and functions to the point an error (or any other thing I would
    >like to trace to) occurrs.
    It's just about possible, but you have to do it by hand. You need the
    __LINE__ and __FUNCTION__ magic constants. Eg

    function makeerror ($errorstring, $function, $line)
    {
    die ("Error: $errorstring on line $line in $function");
    }

    makeerror("Can't connect to database", __FUNCTION__, __LINE__);

    I have a fairly standard way of handling errors in classes that I can
    discuss in more detail, if you want.



    Wouter Van Vliet Guest

  2. #2

    Default RE: [PHP] Determining where a function is called from..

    At 2003-08-15 16:35 +0200, Wouter van Vliet wrote:
    >I noticed the __LINE__ and __FUNCTION__ magic constants too, but they don't
    >really contain the values I was looking for, as they have the current
    >function and line in it. But what I want my Logging class (I have one too
    >:D) to display is what I am about to illustrate with an example:
    >
    >script: index.php
    >-----------------
    > 01 $Database->connect('host', 'database', 'username', 'password');
    > (...)
    > 20 $Database->execute('DELETE news WHERE id IN ()');
    >
    > -> this would produce an error in my Database class, error in sql syntax
    >
    >...etc.
    Have you studied this:
    [url]http://www.php.net/manual/en/function.debug-backtrace.php[/url]

    You may also want to invoke phpinfo() at the place the
    error is occuring.

    There are more nice functions on:
    [url]http://www.php.net/manual/en/ref.errorfunc.php[/url]

    Greetings,
    Jaap

    Jaap Van Ganswijk Guest

  3. #3

    Default Re: [PHP] Determining where a function is called from..

    Use debug_backtrace():

    if($error=mysql_error()) {
    $bt=debug_backtrace();
    echo $bt[0]['file'].':'.$bt[0]['line'].'<br>'
    .$error.'<br>'.$query;
    }

    hope i got everything right

    Wouter van Vliet wrote:
    > Thanks David,
    >
    > I noticed the __LINE__ and __FUNCTION__ magic constants too, but they don't
    > really contain the values I was looking for, as they have the current
    > function and line in it. But what I want my Logging class (I have one too
    > :D) to display is what I am about to illustrate with an example:
    >
    > script: index.php
    > -----------------
    > 01 $Database->connect('host', 'database', 'username', 'password');
    > (...)
    > 20 $Database->execute('DELETE news WHERE id IN ()');
    >
    > -> this would produce an error in my Database class, error in sql syntax
    >
    > script: class.database.inc.php
    > ------------------------------
    > 2 class Database {
    > 3
    > 4 var $C; # Connection resource
    > 5 var $S; # Settings
    > 6 var $L; # Log Object
    > 7 var $R; # Result
    > 8
    > 9 function Database($Host, $Database, $Username, $Password) {
    > 10 global $Log;
    > 11
    > 12 $this->S = Array( 'host' => $Host,
    > 13 'database' => $Database,
    > 14 'username' => $Username,
    > 15 'password' => $Password
    > 16 );
    > 17
    > 18 $this->L = $Log;
    > 19
    > 20 return true;
    > 21 }
    > (...)
    > 55 function execute() {
    > 56 $this->connect(false);
    > 57
    > 58 # Prepare
    > 59 $Query = $this->prepare(func_get_args());
    > 60 $this->dblog($Query);
    > 61
    > 62 # Execute the query
    > 63 $this->R['resource'] = mysql_query($Query,
    > $this->C) or $this->dberror();
    > 64 $this->R['affectedrows'] =
    > mysql_affected_rows($this->C) or $this->dberror();
    > 65
    > 66 return $this->R['affectedrows'];
    > 67 }
    > (...)
    > 171 function dberror() {
    > 172 if ($String = mysql_error($this->C)) {
    > 173 $OldType = $this->L->setLogType(L_STDOUT);
    > 174 $this->L->Error('[database] '.$String);
    > 175 $this->L->setLogType($OldType);
    > 176 };
    > 177 }
    > 178 };
    >
    > I could fairly live with passing __LINE__ and __FUNCTION__ at some places,
    > but not in my index.php (seperating front- and backend). What I want
    > dberror() to pass to my logging module is something like "You have an error
    > in your sql syntax at line 1, called from index.php on line 20". The first
    > part being just the mysql error, after the comma from where the errorfull
    > query was called ..
    >
    > Thanks once more,
    > Wouter
    >
    > Ps. or am I really wanting too much now?
    >
    > -----Oorspronkelijk bericht-----
    > Van: David Otton [mailto:phpmailjawbone.freeserve.co.uk]
    > Verzonden: vrijdag 15 augustus 2003 16:13
    > Aan: Wouter van Vliet
    > Onderwerp: Re: [PHP] Determining where a function is called from..
    >
    >
    > On Fri, 15 Aug 2003 16:02:24 +0200, you wrote:
    >
    >
    >>I've got this problem .. not really a problem but still some thing to my
    >>disliking. For a project I wrote some database handling class, no big deal.
    >>When a function from this module is called with a query containing an
    >
    > error,
    >
    >>or some other error occurs inside this class it reports to my that an error
    >>occured inside that class. "Line blabla of class.database.inc.php". But
    >
    > what
    >
    >>I really want to know is on what line the $Database->select() or such was
    >>called so that I can effectively trace the error.
    >>
    >>So, for effective error reporting I would like to know if there are
    >>functions or constants containing
    >> - the script-name a user-function is called from
    >> - the linenumber of this script a user-function is called from
    >>
    >> - And maybe it would even be doable to get an entire stack-trace of files
    >>and lines and functions to the point an error (or any other thing I would
    >>like to trace to) occurrs.
    >
    >
    > It's just about possible, but you have to do it by hand. You need the
    > __LINE__ and __FUNCTION__ magic constants. Eg
    >
    > function makeerror ($errorstring, $function, $line)
    > {
    > die ("Error: $errorstring on line $line in $function");
    > }
    >
    > makeerror("Can't connect to database", __FUNCTION__, __LINE__);
    >
    > I have a fairly standard way of handling errors in classes that I can
    > discuss in more detail, if you want.
    >
    >
    >
    >
    Marek Kilimajer Guest

  4. #4

    Default RE: [PHP] Determining where a function is called from..

    Nope .. I hadn't studiet the ref.errorfunc.php in this search.. I did some
    other time and the backtrace function didn't stick around in my head.

    Thank you all for helping me .. of course, debug_backtrace() was what I
    needed ;) .. what a magnificent new function in PHP4.3 that is.. Only too
    bad my vim syntax file doesn't yet recognize it..

    Wouter


    -----Oorspronkelijk bericht-----
    Van: Jaap van Ganswijk [mailto:ganswijkxs4all.nl]
    Verzonden: vrijdag 15 augustus 2003 16:51
    Aan: [email]php-generallists.php.net[/email]
    Onderwerp: RE: [PHP] Determining where a function is called from..


    At 2003-08-15 16:35 +0200, Wouter van Vliet wrote:
    >I noticed the __LINE__ and __FUNCTION__ magic constants too, but they don't
    >really contain the values I was looking for, as they have the current
    >function and line in it. But what I want my Logging class (I have one too
    >:D) to display is what I am about to illustrate with an example:
    >
    >script: index.php
    >-----------------
    > 01 $Database->connect('host', 'database', 'username',
    'password');
    > (...)
    > 20 $Database->execute('DELETE news WHERE id IN ()');
    >
    > -> this would produce an error in my Database class, error in sql
    syntax
    >
    >...etc.
    Have you studied this:
    http://www.php.net/manual/en/function.debug-backtrace.php

    You may also want to invoke phpinfo() at the place the
    error is occuring.

    There are more nice functions on:
    http://www.php.net/manual/en/ref.errorfunc.php

    Greetings,
    Jaap


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


    Wouter Van Vliet Guest

Similar Threads

  1. a function is not called (simple)
    By Québec in forum Macromedia Flash Actionscript
    Replies: 3
    Last Post: February 27th, 06:57 PM
  2. URG... Function getting called multiple times for no reason
    By Karim N webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 2
    Last Post: February 3rd, 06:08 PM
  3. Determining identity of a client from a service called from an ASP.NET page
    By news.microsoft.com in forum ASP.NET Security
    Replies: 0
    Last Post: December 10th, 12:40 PM
  4. Determining where a function is called from..
    By Wouter Van Vliet in forum PHP Development
    Replies: 0
    Last Post: August 15th, 02:02 PM
  5. ASCX - Function Not Being Called
    By George Durzi in forum ASP.NET General
    Replies: 4
    Last Post: June 26th, 06:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139