Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts

    What's the benefit of abstract classes and interfaces?

    Hi there,

    I've just read the PHP's manual abstract classes and interface chapters. I still wonder why should I use them? What's the benefit of using abstract classes or using interfaces?
    PHP 5 & MySQL 5 (Y)

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Both of these serve great purposes. Its more difficult to explain in a language like PHP since it is datatype weak, but I'll try nonetheless.

    An abstract is just like a class. Its like saying 'this is a skeleton for what is to be expected of any object for this type'. It can contain concrete methods for logic that does not change, or changes very little for any of the child classes. Child classes can override any of these methods (except in PHP 5.0.x, where Private children are overridden by their parents... [bug] grrr...). Abstract methods can also be included, which is the same as 'I know you're children will need this method, but its different for each child; let them worry about it'. Abstracts cannot be instantiated, but you can create final constructors if you want an abstract singleton pattern factory for example.

    Since OOP logic dictates that a class can only extend one other class, interfaces are used to provide 'filler' methods for expected functionality. It is once again a skeleton, and simply says 'If you implement me, you need to include these methods'.

    This is where it gets tricky in PHP explanation. Strong datatype languages like C++, C# and java declare their variables by telling it what type it requires. This allows the compilier or interpreter to allocate a certain amount of space in memory for this type of data. PHP needs to do these checks with keywords like instanceof or get_class/interface comparisons.

    Consider the following:
    PHP Code:
    interface IComparable
    {
        public function 
    compareTo(IComparable $other);
    }

    class 
    MyClass implements IComparable
    {
        private 
    $key;
        public function 
    compareTo(IComparable $other)
        {
            
    // PHP doesn't support generics, so we need to check the type ourselves
            
    $class get_class($this); // Or just get_class() is fine.
            
    if (!($other instanceof $class))
            {
                throw new 
    Exception(sprintf("Class %s cannot be compared to type %s!\n"get_class(), get_class($other));
            }
            return (int)
    $this->key - (int)$other->key;
        }

    The interface serves the purpose of guaranteeing that compareTo exists on any object of type IComparable. In Java or C# you can store this as a type of IComprable:
    Code:
    public static void main(String args[])
    {
        IComparable i1 = new MyClass(1);
        IComparable i2 = new MyClass(2);
        System.out.println("i1.compareTo(i2)? " + i1.compareTo(i2));
    }
    If myclass cannot be stored as an IComparable, runtime will fail on the new MyClass calls, and if I'm not mistaken the compiler will fail on the i1.compareTo call.

    I hope this helps. OOP takes a lot of practice to really determine how/what/when to use abstracts and interfaces. Good choices in UML Class design will really provide you with great flexibility in you're object designs.
    Last edited by Fou-Lu; 08-06-2008 at 04:29 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    kaisellgren (08-06-2008)

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Wow thanks.. I still think I need to buy a book in Finnish :/
    PHP 5 & MySQL 5 (Y)

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Any OO concept book would work. Each language has their own rules, but the overall picture is pretty much the same.
    I started natively as a procedural programmer with Perl, PHP and C. I moved into OO languages much later. It took me probably a year before I recognized the key benefits of using abstracts and interfaces - especially interfaces. One day I just started using them more and more, and now I pretty much use them everywhere. PHP will make me very happy once they support nested classes and object generics. Whether zend will do that is an entirely different matter, but I'm still keeping my fingers crossed for those.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •