sunilvadranapu Posted March 18, 2008 Share Posted March 18, 2008 Hi, I have developed one PHP class- DBConnection to connect Database and another class-Logger for logging the information like connection success or failure and event information. Now how can i access WriteLog method of Logger class in connect() method of DBConnection Class. Can you give me sample code for this. Thanks in advance --Sunil Quote Link to comment Share on other sites More sharing options...
Jeremysr Posted March 18, 2008 Share Posted March 18, 2008 I think you do it like this: class DBConnection { function connect() { Logger::WriteLog(); } } Quote Link to comment Share on other sites More sharing options...
Barand Posted March 18, 2008 Share Posted March 18, 2008 or <?php class ErrorLogger { function writeError($error) { // log the error echo '<pre>', $error, '</pre>'; } } class DBConnector { private $logger; private $cnx; function __construct () { $this->logger = new ErrorLogger(); } function connect($host, $user, $pwd, $dbname) { $this->cnx = @mysql_connect ($host, $user, $pwd); if (!$this->cnx) { $this->logger->writeError(mysql_error()); return false; } if (!mysql_select_db ($dbname)) { $this->logger->writeError(mysql_error()); return false; } } } $dbc = new DBConnector; if (!$dbc->connect('localhost','xxx','yyy','tst') ) { exit('Database error'); } ?> Quote Link to comment Share on other sites More sharing options...
sunilvadranapu Posted March 18, 2008 Author Share Posted March 18, 2008 Thanks for your replies and they are very helpful to me. Thanks for your support. -sunil Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.