Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract keyword in PHP

Hey, I'm quite experienced with PHP but I have no idea what the keyword abstract does when it comes down to object orientated programming. Can anyone explain in plain english what it can be used for?

What situations would I use the abstract keyword in? How does it change the class/interface?

like image 797
Johnny Avatar asked Dec 05 '09 12:12

Johnny


People also ask

Why do we use abstract class in PHP?

We use abstract classes when we want to commit the programmer (either oneself or someone else) to write a certain class method, but we are only sure about the name of the method, and not the details of how it should be written. To take an example, circles, rectangles, octagons, etc.

What is an abstract function?

An abstract function has no implemention and it can only be declared on an abstract class. This forces the derived class to provide an implementation. A virtual function provides a default implementation and it can exist on either an abstract class or a non-abstract class.

What is abstract and interface in PHP?

Interface are similar to abstract classes. The difference between interfaces and abstract classes are: Interfaces cannot have properties, while abstract classes can. All interface methods must be public, while abstract class methods is public or protected.

Does PHP support abstract class?

PHP has abstract classes and methods. Classes defined as abstract cannot be instantiated, and any class that contains at least one abstract method must also be abstract.


3 Answers

(Hope this is simple enough -- I don't think I can do better ^^ )

An abstract class can not be instanciated : you can only create another class that inherits from the abstract class, and instanciate that child class.

And if you declare some methods as abstract, those must be defined in the child class, for that one to be instanciable.

like image 66
Pascal MARTIN Avatar answered Oct 21 '22 10:10

Pascal MARTIN


Declaring a class abstract means that it must be subclassed in order to be used. An abstract class can not be instantiated. One can see it as an extended interface that might include implementation code (as opposed to an interface).

By declaring a method abstract, you force the sub class to implement the method.

like image 21
hanse Avatar answered Oct 21 '22 11:10

hanse


The definition is mentioned above, now I will try to give you an example:

"abstract" ensures that you follow a specific logic, e.g. a ticket's material is ALWAYS "paper", or a creditcard must always have a "code". This is important if you work in a big company which has strict standardisation or if you want to 'force' your developers to follow a specific structure, so their code won't end up in a mess.

    abstract class ticket{

    public function material()
    {
        return 'Paper';
    }

}

abstract class creditcard{

    public function material()
    {
        return 'Plastic';
    }

    abstract function setCode(); // the ";" semicolon is important otherwise it will cause an error

}

class key extends ticket{

    public function getMaterial()
    {
        return parent::material();
    }
}

class anotherKey extends creditcard{

    public function setCode($code)
    {
        $this->code = $code;
    }
}

If we do not define the "setCode" method the parser will return an error on "new anotherKey"

like image 20
Julius F Avatar answered Oct 21 '22 11:10

Julius F