Activity Stream
48,167 MEMBERS
6876 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Results 1 to 10 of 10
  1.     
    #1
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com

    Default [PHP] Interfaces

    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 Code: 
    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 Code: 
    class MyClass Implements SomeInterface
    {
         

    Full example of how there used:

    Interface:
    PHP Code: 
    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 Code: 
        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 Code: 
        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 Code: 
    //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
    litewarez Reviewed by litewarez on . [PHP] Interfaces 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: interface Sample Rating: 5
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  2.   Sponsored Links

  3.     
    #2
    Member
    Thank u lite nicely explained.

  4.     
    #3
    Member
    Website's:
    Doxsters.net
    I kinda get it, but could you give an example of a function in class MySqli, eg how would you use/modify the functions in IDatabase?

    Need a Designer/Web Developer? Click Me

    MSN: PM me for it.
    Email(Preferred):timtamboy63@gmail.com

    "Power Corrupts. Absolute Power Corrupts Absolutely"

  5.     
    #4
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    Update with more code and an extra block.
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  6.     
    #5
    Member
    Website's:
    Doxsters.net
    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?

    Need a Designer/Web Developer? Click Me

    MSN: PM me for it.
    Email(Preferred):timtamboy63@gmail.com

    "Power Corrupts. Absolute Power Corrupts Absolutely"

  7.     
    #6
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    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 Code: 
    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 ElectricalDeviceFrequencyTuner{
      
    // define ElectricalDevice...
      // define FrequencyTuner...
    }

    class 
    CellPhone implements ElectricalDeviceFrequencyTuner{
      
    // 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.
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  8.     
    #7
    Member
    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 Code: 
    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 Code: 
    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.

  9.     
    #8
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    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 Code: 
    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 Code: 
    class Array implements Iterator
    {
        
    //...methods

    Then within my user interfaces i can specifically expect a String or an Array type.
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  10.     
    #9
    Member
    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

  11.     
    #10
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    yea but having methods directly available for data types is very nice, but yea PHP is not the best language to do this.
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

BE SOCIAL