Jump to content

How to access method of another class


sunilvadranapu

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.