What are they?
Interfaces a block-less classes that are used to make sure other classes contain certain methods and structure, the reason we use these is so that when we have a set of classes in projects we make sure that no matter what the method will exists.
What do they look like?
Here is an example:
That is it, there is no code just method deceleration.
How there used
Whan you create a class you use the keyword "implements" to tell the class that its using an interface.
Example:
Full example of how there used:
Interface:
So the interface has the basics in there for any database engine you would be using.
so for the classes now you can have multiple classes the inherit the structure from the interface.
MySql
MySqli
No as you can see that you can have unlimited amount of database classes but they all must contact the exact methods specified in the interface, this means that no matter what database structure you use, your application will still work the same
So usage lets say
Hope you can understand a little bit more xD
Interfaces a block-less classes that are used to make sure other classes contain certain methods and structure, the reason we use these is so that when we have a set of classes in projects we make sure that no matter what the method will exists.
What do they look like?
Here is an example:
PHP:
interface Sample
{
public function Method($value);
}
That is it, there is no code just method deceleration.
How there used
Whan you create a class you use the keyword "implements" to tell the class that its using an interface.
Example:
PHP:
class MyClass Implements SomeInterface
{
}
Full example of how there used:
Interface:
PHP:
interface IDatabase
{
public function connect($host,$username,$password);
public function openDatabase($database);
public function query($query);
}
So the interface has the basics in there for any database engine you would be using.
so for the classes now you can have multiple classes the inherit the structure from the interface.
MySql
PHP:
class DBMySql implements IDatabase
{
private $Connection,$Query;
public function connect($host,$username,$password)
{
$this->Connection = mysql_connect($host,$username,$password,true);
}
public function openDatabase($database)
{
mysql_select_db($database);
}
public function query($query)
{
return $this->Query = mysql_query($query);
}
}
MySqli
PHP:
class DBMySqli implements IDatabase
{
private $Connection,$Query;
public function connect($host,$username,$password)
{
$this->Connection = new MySqli($host,$username,$password,true);
}
public function openDatabase($database)
{
$this->Connection->select_db($database);
}
public function query($query)
{
return $this->Query = $this->Connection->query($query);
}
}
No as you can see that you can have unlimited amount of database classes but they all must contact the exact methods specified in the interface, this means that no matter what database structure you use, your application will still work the same
So usage lets say
PHP:
//MySql
$DBMySql = new DBMySql ();
//MySqli
$DBMySqli = new DBMySqli();
//No matter witch one i use they would always have the same functions, so $DBMySql->connect does the same thing as $DBMySqli->connect, apart from it uses different inner logic...
//BUT THEY WILL ALLWAYS BE THE SAME TO THE USER.
Hope you can understand a little bit more xD