Jump to content

Recommended Posts

Hey,

 

I have been reading a book on OO PHP, to bring up my knowledge can some please check my understanding of both Abstract Class & Interfaces.

 

Abstract Class - Basically cannot be instantiated(generated via an object), they define methods(functions) but cannot implement them. You use the Abstract keyword to define a class.

 

Interfaces - Basically specify common implementation of methods for example

public function setVariable($name, $var);

You would then expand on this in another class. The purpose of them is to have genricly defined implementation methods.

 

Is that correct? Have I missed anything?

 

Also can anyone give me some good examples, of how and when to use them?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/239367-abstract-classes-interfaces/
Share on other sites

Interfaces define, well, an interface.  Method signatures which must be fleshed out in any class which implements them.  They're sort of a way around PHP (and other languages) not allowing true multiple inheritance.  They're also used to foster polymorphism.  Much like an abstract class, an interface denotes a is-a relationship.  An object of a class that implements an interface is seen as an instance of that interface.  So, something like:

 

class Example extends Parent implements ISomethingElse {}

$example = new Example();

 

Would be considered to be an instance of Example, Parent, and ISomethingElse to PHP.  Really useful if you have something like:

 

class CashRegister
{
   public function calculateTotal(ISomethingElse $items)
   {
      $total = 0;

      foreach($items as $item)
      {
         $total += $item->getPrice();
      }

      return $total;
   }
}

 

Examples are obviously canned, but interfaces definitely have their uses.  Take a look at the Observer Pattern as another example.

Interfaces - A class that implements an interface must implement all of the public (which is an implicit method declaration) methods from the interface.  It's basically a generic template for any class that implements it.  You can add additional functions if you wish.

 

Example - *Note* this was for work so it's in Java, but it was recent and you should get the idea.  We have indexes from electronic books that are transformed into either palmindexes or sqliteindexes.  The common theme here is that they are both indexes and will always use generic 'index methods'.

 

public interface Index
{
    /*  
     * @return the number of entries in this index
     */
    public int getSize();

    /*  
     * @return the position in this index of the first entry that starts with entry and -1 if it is not found
     */
    public int findEntry(String entry);

    /*  
     * fill a list with the indexenries that startwith or match the entry up to a max of max for sql indexs this will be looked for in the entire database not just this index
     */
    //vector 
    public int findEntries(Hashtable results, String entry, int max);
.
.
.
//blah blah blah, a bunch more methods
}

 

Now I have two more classes named PalmIndex.java and SQLiteIndex.java that both implement Index

public class PalmIndex implements Index
{
    // Constructors

    public int getSize()
    { // TODO: Auto-generated method stub }
    public int findEntry(String entry)
    { // TODO: Auto-generated method stub }
    public int findEntries(Hashtable results, String entry, int max)
    { // TODO: Auto-generated method stub }
}

 

public class SQLiteIndex implements Index
{
    // Constructors

    public int newMethod()
    { // Additional method that I need for sqlite, you can add as many as you want }
    public int getSize()
    { // TODO: Auto-generated method stub }
    public int findEntry(String entry)
    { // TODO: Auto-generated method stub }
    public int findEntries(Hashtable results, String entry, int max)
    { // TODO: Auto-generated method stub }
}

 

That way, when I got to decide which one to use, I can just create an instance of the Index interface.  You can make different and multiple constructors if you wish (that's why the PalmIndex instance has more parameters).  This would be used as so:

 

public static Index blahMethod(String catname)
{
   Index index = null;
   if (SearchView.inDB(catcode))
   {
      index = new SQLiteIndex(path, Integer.parseInt(catcode));
   }
   else
   {
      index = new PalmIndex(resDB, catname, Integer.parseInt(catcode), indexnum);
   }
   return index;
}

 

Interfaces are very important in Java because the language does not support Multiple Inheritence (I don't think PHP does either) so you can extend multiple Abstract classes and implement a single interface to circumvent this caveat, if you will.

 

Again, sorry it's in Java, I can easily port it to PHP if you want.  I'll write up an Abstract post a little later as well.

Okay, so my understanding of abstract classes is fine and interfaces was sort of correct then.

 

So for example, if I had the following;

<?php
interface person
{
   public function name();

}
?>

 

I can then go expand on that interface in a class, be it abstract, normal or another interface. Then pump information into the method name to get it to do loads of neat stuff. But if I use this interface within a class, it has to have something in and cannot be blank?

 

Patterns are later on in the book, i'm trying to read, re-read to ensure I get the core features etc... down to a tee!!

 

Thanks for the help!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.