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 Link to comment https://forums.phpfreaks.com/topic/96652-how-to-access-method-of-another-class/ 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(); } } Link to comment https://forums.phpfreaks.com/topic/96652-how-to-access-method-of-another-class/#findComment-494606 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'); } ?> Link to comment https://forums.phpfreaks.com/topic/96652-how-to-access-method-of-another-class/#findComment-494643 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 Link to comment https://forums.phpfreaks.com/topic/96652-how-to-access-method-of-another-class/#findComment-494660 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.