Interface

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shafiq2626
Forum Commoner
Posts: 88
Joined: Wed Mar 04, 2009 1:54 am
Location: Lahore
Contact:

Interface

Post 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
Last edited by Benjamin on Tue May 26, 2009 11:02 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Interface

Post 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?
shafiq2626
Forum Commoner
Posts: 88
Joined: Wed Mar 04, 2009 1:54 am
Location: Lahore
Contact:

Re: Interface

Post 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
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Interface

Post 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
Post Reply