Abstract Classes in PHP
Abstract Classes in PHP
Abstract classes and methods are introduced in PHP 5 An abstract class is a class which will usually be used as a base class for other classes to inherit from
An abstract class is a class with or without data members that provides some functionality and leaves the remaining functionality for its child class to implement.
The child class must provide the functionality not provided by the abstract class or else the child class also becomes abstract. If a class contains abstract method then the class must be declared as abstract. Any method which is declared as abstract must not have the implementation part but the declaration part only. The child classes which inherits the property of abstract base class, must define all the methods declared as abstract.
Objects of an abstract and interface class cannot be created i.e. only objects of concrete class can be created
To define a class as Abstract, the keyword abstract is to be used In the class example, the method getPrice() in class Furniture has been declared as Abstract. This means that its the abstract Class_Name { of the child //responsibility insert attribute definitions here class to provide the functionality of getPrice(). The BookShelf class is a child of the // insert method definitions here Furniture class and hence provides the function body for getPrice(). } The child classes which inherits the property of abstract base class, must define all the methods declared as abstract. An abstract method has no body
class BookShelf extends Furniture Example of Abstract Class { abstract class Furniture private $price; { private $height, width, length; public setData($h, $w, $l, $p) public function setData($h, $w, $l) { { parent::setData($h, $w, $l); $this->height = $h; $this->price = $p; $this->width = $w; } $this->length = $l; //this is the function body of the parent abstract method } public function getPrice() //this function is declared as abstract and hence the function { //body will have to be provided in the child class return $this->price;
public abstract function getPrice(); }
} }
If a method is defined as abstract then it cannot be declared as private (it can only be public or protected). This is because a private method cannot be inherited.
On the other hand, if a class is not declared as abstract, it cannot contain any abstract methods You can create a new abstract class that extends an earlier one abstract class MyAbstractBaseClass { public abstract function doSomething();
}
abstract class MyAbstractSubClass extends MyAbstractBaseClass {} Abstract classes ensure conformity because any classes derived from them must implement all abstract methods derived within the class. Attempting to forgo implementation of any abstract method defined in the class results in a fatal error.
Characteristics of Abstract Classes 1. Single inheritance. Child classes can extend only one class at a time.
7. The visibility of functions in the child classes must be the same or less restrictive than the parent class. Thus, a
protected abstract function can be implemented as either protected or public but not private. 8. Declaring functions as static abstract throws a strict warning in PHP 5.2 or earlier, however, as of PHP 5.3 this is allowed.
Characteristics of Abstract Classes 1. Single inheritance. Child classes can extend only one class at a time.
7. The visibility of functions in the child classes must be the same or less restrictive than the parent class. Thus, a
protected abstract function can be implemented as either protected or public but not private. 8. Declaring functions as static abstract throws a strict warning in PHP 5.2 or earlier, however, as of PHP 5.3 this is allowed.