Hello to all first of all.
This is Scorpionz. I am working on PHP OOPS these days, I am having a little problem in my Connection class, with Database.

Lets have a a look.

interface db
{
	public function connect();
	public function close();
	public function error();
	
}

class mysqldb implements db
{
	private $server;
	private $username;
	private $password;
	private $dbname;
	private $link;
	
	public function connect($server,$username,$password)
	{
		$this->link=mysql_connect($server,$username,$password);
	}
	public function close()
	{
		return mysql_close($this->link);
	}
	public function error()
	{
		return mysql_error($this->link);
	}
}

$MysqlDb	=	new mysqldb();
$MysqlDb->connect('localhost','root','');

I am having an error in this way:

Fatal error: Declaration of mysqldb::connect() must be compatible with that of db::connect() in C:\xampp\htdocs\sample_test\classes\config.php on line 10

I am unable to understand where am i wrong.
Any Response will be highly appreciated.

Thanks and Regards
Scorpionz.

You need to add your functions parameters to the function in the interface.

interface db
{
	public function connect($server,$username,$pasword);
	public function close();
	public function error();
	
}

Thanks for reply l buddy.
but unfortunately same error as mentioned in my first Post.

Any other changes?

Regards.

Its done now have a look:

interface DB 
{ 
    public function connect(); 
    public function error();
    public function selectdb(); 
    public function close(); 
   
} 

class MySqlDB implements DB 
{
	private  $link; 
        private $server;
        private $username;
        private $password;
        private $dbname;
        
        
        public function connect($server='', $username='', $password='') 
        { 
            $this->link = mysql_connect($server, $username, $password); 
        } 
        
        public function selectdb($dbname='')
        {
        	$this->link	= mysql_select_db($dbname);
        }
        
        public function error() 
        { 
            return mysql_error($this->link); 
        }
		public function close() 
        { 
            return mysql_close($this->link); 
        } 
        
}

This will make you whenever you migrate to any other Db, you just need to create an object of class and let it done.

Regards
Scorpionz.

interface db
{
	public function connect($server,$username,$pasword);
	public function close();
	public function error();
	
}

class mysqldb implements db
{
	private $server;
	private $username;
	private $password;
	private $dbname;
	private $link;
	
	public function connect($server,$username,$password)
	{
		$this->link=mysql_connect($server,$username,$password);
	}
	public function close()
	{
		return mysql_close($this->link);
	}
	public function error()
	{
		return mysql_error($this->link);
	}
}

$MysqlDb	=	new mysqldb();
$MysqlDb->connect('localhost','root','');

I believe this should work.

interface db
{
    public function connect($server,$username,$pasword);
    public function close();
    public function error();

}

Hi

except to the little typo snucked in, the third variable in function connect, the $pasword should be $password.

Cheeerz

Edited 4 Years Ago by rsiljat: edit

This article has been dead for over six months. Start a new discussion instead.