Accounting for line numbers...

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
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Accounting for line numbers...

Post by Maugrim_The_Reaper »

I suppose to start with the hypothetical situation...

Let's say I have two classes. One is a simple data access object, with a number of methods which interact with the database, e.g.

Code: Select all

<?php

   class UserDA {

      // assume here we set UserDA::db to point to a singleton instance of DB_ADODB (see below)
       // this accepts an object of type User (an object holding row data for a single database record, and getter methods)

      public function create(User $user) {
         $this->db->exec(__LINE__,__FILE__,"insert into table_users (name, address) values ('".$user->getName()."','".$user->getAddress()."')");
      }

   }

?>
I also have a simple DB class (which delegates to ADODB for example, but adds some degree of extra information for debugging purposes.

Code: Select all

<?php

   class DB_ADODB {

      // assume up here we set a DB_ADODB::dbA property to reference a ADODB object

      public function exec($line, $file, $sql, $conf=null) {
        $result = $this->dbA->Execute($sql);
        if(!$result) {
           die("Query failed on line $line of $file.");
        }
      }

   }

?>

In the above I'm passing the line number and filename of exactly where a SQL call is made in the UserDA class. The idea being that if an error occurs, the application will die, and output a message telling me exactly where the SQL query responsible for the error occured. (Forgive any obvious shortcomings in the above - it's a simple case ignoring stuff like mysql error strings, etc.)

My question is whether there exists a method of capturing these __LINE__, __FILE__ values to pass to any subsequent message generated by another method, in another class - but without passing them through the function.

If you follow my logic, you wind up passing them with every method call to the DB_ADODB class - call me a lazy typist ;). Curious whether there's other alternatives to achieve the same objective (knowing which sql query giving rise to error, and its location in code) without passing line/file explicitly.

Just might be there is no other way, and this is a wasted post... If so I'm hunting whoever mentioned an alternative exists and make them eat their socks...
Last edited by Maugrim_The_Reaper on Sat Sep 03, 2005 9:39 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Now who's eating the socks? ;)

Thanks - should have been obvious. Time to put it to use however...
Post Reply