Page 1 of 1

Accounting for line numbers...

Posted: Sat Sep 03, 2005 9:33 am
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...

Posted: Sat Sep 03, 2005 9:38 am
by feyd

Posted: Sat Sep 03, 2005 9:47 am
by Maugrim_The_Reaper
Now who's eating the socks? ;)

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