Status
Not open for further replies.

litewarez

Active Member
1,367
2008
1
0
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:

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
 
9 comments
Hm I still don't get it, Wouldn't it be the exact same whether you had 'Implements IDatabase' in there or not? After all, you are redifining the functions right?

I guess it might be useful for backwards compatibility, but you don't need the interface for that do you?
 
you do, take your database class and the method i posted afterwords.

you have a database layer and then sub layers such as mysql, oracle etc

lets say they all implemented a interface that made sure that they worked ok with the database class, and then the user wanted to add another one for lets say xml, as long as they implement the interface it would be fully compatible with the database class.


another example:
PHP:
interface ElectricalDevice{
  public function power_on();
  public function power_off();
}

interface FrequencyTuner{
  public function get_frequencey();
  public function set_frequency($f);
}

class ElectricFan implements ElectricalDevice{
  // define ElectricalDevice...
}

class MicrowaveOven implements ElectricalDevice{
  // define ElectricalDevice...
}

class StereoReceiver implements ElectricalDevice, FrequencyTuner{
  // define ElectricalDevice...
  // define FrequencyTuner...
}

class CellPhone implements ElectricalDevice, FrequencyTuner{
  // define ElectricalDevice...
  // define FrequencyTuner...
}

the best thing this does is prevent your application from being error prone as everything is specific and exact, if you created lots of lose code your more likely to have errors.
 
Well, when you are declaring interfaces you should also specify parameters data type (in case its instance of another class, in case it is just a string or integer it doesnt matter)

PHP ignore it however interfaces are here generally for making extensions, modules and plugins into applications. This makes much easier understand application design.

PHP:
interface IDatabaseAdapter
{
  public function query(IDatabaseQuery $query);
}

Now we know that method query expects instance of class based on interfaces IDatabaseQuery. This is meant for DB layer which has one main class and uses adapters (drivers) for different databases (mysql, mssql, pgsql, mysqli, etc..)


Btw, you can also inherit interfaces
PHP:
interface IAdapter
{
  public function register();
}

interface IDatabaseAdapter extends IAdapter
{
  public function query(IDatabaseQuery $query);
}

Now when you create class which implements interface IDatabaseAdapter you have declare both methods (register and query) or you get Fatal Error i think, or parse error, iam not sure atm.
 
Specifying the datatype is not really needed unless your passing in classes / object but if i was do do that i would create a data type class for each datatype

PHP:
class String implements Multilingual
{
     public $value;
     function __construct($string)
     {
         $this->value = $string;
     }
     function __toString($string)
     {
          return $this->value;
     }

     function __call($method,$args)
     {
          if(function_exists($method))
          {
              $this->value = $method($this->value);
          }
          return $this;
     }
}
$myString = new String("Hello World");
$myString->trim();

PHP:
class Array implements Iterator
{
    //...methods
}

Then within my user interfaces i can specifically expect a String or an Array type.
 
I wouldnt do that. Since php is case-nonsensitive it might cause some troubles, but last time i tried it was ok i think, maybe small issues, but worked. And this solution would be too .NETish XD
 
Status
Not open for further replies.
Back
Top