Page 1 of 1

Interface

Posted: Tue May 26, 2009 6:31 am
by shafiq2626
what is the benefits of interface.
i make this interface.
like

Code: Select all

interface DB 
{ 
    public function connect(); 
public function query($query); 
    public function fetch_array($result); 
}
 
class MySqlDB implements DB
    { 
        private  $link; 
         
        public function connect($server='', $username='', $password='', $new_link=true, $client_flags=0) 
        { 
            $this->link = mysql_connect($server, $username, $password, $new_link, $client_flags);
             
        } 
public function query($query) 
        { 
            return mysql_query($query, $this->link); 
        } 
         
        public function fetch_array($result, $array_type = MYSQL_BOTH) 
        { 
            return mysql_fetch_array($result, $array_type); 
        } 
}
 
$db = new MySqlDB; 
    $db->connect('localhost', 'root', '123'); 
$db->query('dvdstore');
 $result = $db->query("SELECT * FROM actress"); 
         
    while($row = $db->fetch_assoc($reuslt)) { 
        echo($row['Name']); 
    }  

so if i remove implements DB.
then no effect on result same functionality work.
then why need make an interface.
can any one help me with thanks

Re: Interface

Posted: Tue May 26, 2009 7:23 am
by jaoudestudios
Its similar to an abstract class. It gives you some control over child classes.

In the Class than implements DB, you must define the 3 methods: connect, query, fetch_array.

If you dont need an interface why are you using it?

Re: Interface

Posted: Tue May 26, 2009 10:53 pm
by shafiq2626
Actually i wanted to know the purpose of interface.
Now i become know that interface is like your template. As you design a template to create any web application. then you know what i am to do according to this template.
Similarly when you are going to making classes then you make interface to know that what i am to do.
ok

Re: Interface

Posted: Wed May 27, 2009 2:03 am
by jaoudestudios
shafiq2626 wrote:Actually i wanted to know the purpose of interface.
"In the Class than implements DB, you must define the 3 methods: connect, query, fetch_array." - mentioned above
Any class that implements an interface must define all methods defined in the interface class.

http://us.php.net/interfaces