tags:

views:

29

answers:

1

Hello,

Any real world simple samples of using abstract class? I'm trying to get in PHP's OOP, but I still can't understand - why abstract class should be used and when (Yes, I know that it's impossible to create abstract class instance, only instance of class inheriting it).

Thank you

A: 

Maybe you have an image class, and you have 2 drivers, GD and ImageMagick.

Your base class may be

abstract class Image {

    public function resize($width, $height) {
        // Some prep code...
    }
}

Then your GD driver will be like

class Image_Gd extends Image {
    public function resize($width, $height) {
        // Actual code
    } 
}

Look at Kohana's source on GitHub. It has an Image class which is abstract.

alex
Hm, I thought it is enough to create `interface Image` and make `Image_Gd implements Image`?
Kirzilla
@Kirzilla Well, yes you could do that. Just imagine there is code in that base class.
alex
@alex, got it, thank you!
Kirzilla