PHP Tutorial - PHP Abstract Class






Abstract class defines an abstract concept, for example number is an abstract concept while int, byte are concrete concepts. Vehicle is an abstract concept while car and ship are concrete concepts.

Abstract class is for inheriting to create a new, non-abstract (concrete) class.

By making a parent class abstract, you define the rules as to what methods its child classes must contain.

An abstract method in the parent class doesn't actually have any code in the method.

It leaves that up to the child classes. It specifies what the child class must do, not how to do it.





Syntax

To declare an abstract method, simply use the abstract keyword, as follows:

abstract public function myMethod( $param1, $param2 );

We can optionally specify any parameters that the method must contain.

However, you don't include any code that implements the method, nor do you specify what type of value the method must return.

If you declare one or more methods of a class to be abstract, you must also declare the whole class to be abstract, too:

abstract class MyClass { 
    abstract public function myMethod( $param1, $param2 ); 
}   




Note

You can't instantiate an abstract class-that is, create an object from it-directly:

// Generates an error: "Cannot instantiate abstract class MyClass" 
$myObj = new MyClass;   

Example 1


<?PHP
abstract class Book {
        private $Name;
        public function getName() {
            return $this->Name;
        }
}
?>

As mentioned already, you can also use the abstract keyword with methods, but if a class has at least one abstract method, the class itself must be declared abstract.

You will get errors if you try to provide any code inside an abstract method, which makes this illegal:

<?PHP
abstract class Book {
        abstract function say() {
                print "Book!";
        }
}
?>

This is illegal as well:

<?PHP
abstract class Book {
        abstract function say() { }
}
?>

Instead, a proper abstract method should look like this:

<?PHP
abstract class Book {
        abstract function say();
}
?>

Example 2

The following code shows how to provide implementation for abstract class.


//  w  w w .  java  2  s  .  c  om
<?php

abstract class Shape {
  private $_color = "black";
  private $_filled = false;

  public function getColor() {
    return $this->_color;
  }

  public function setColor( $color ) {
    $this->_color = $color;
  }

  public function isFilled() {
    return $this->_filled;
  }

  public function fill() {
    $this->_filled = true;
  }

  public function makeHollow() {
    $this->_filled = false;
  }

  abstract public function getArea();
}

class Circle extends Shape {
  private $_radius = 0;

  public function getRadius() {
    return $this->_radius;
  }

  public function setRadius( $radius ) {
    $this->_radius = $radius;
  }

  public function getArea() {
    return M_PI * pow( $this->_radius, 2 );
  }
}

class Square extends Shape {
  private $_sideLength = 0;

  public function getSideLength() {
    return $this->_sideLength;
  }

  public function setSideLength( $length ) {
    $this->_sideLength = $length;
  }

  public function getArea() {
    return pow( $this->_sideLength, 2 );
  }
}

class Rectangle extends Shape {
  private $_width = 0;
  private $_height = 0;

  public function getWidth() {
    return $this->_width;
  }

  public function getHeight() {
    return $this->_height;
  }

  public function setWidth( $width ) {
    $this->_width = $width;
  }

  public function setHeight( $height ) {
    $this->_height = $height;
  }

  public function getArea() {
    return $this->_width * $this->_height;
  }
}


class ShapeInfo {
  private $_shape;

  public function setShape( $shape ) {
    $this->_shape = $shape;
  }

  public function showInfo( ) {
    echo "<p>The shape's color is " . $this->_shape->getColor();
    echo ", and its area is " . $this->_shape->getArea() .".</p>";
  }
}


$myCircle = new Circle;
$myCircle->setColor( "red" );
$myCircle->fill();
$myCircle->setRadius( 4 );

$mySquare = new Square;
$mySquare->setColor( "green" );
$mySquare->makeHollow();
$mySquare->setSideLength( 3 );

$info = new ShapeInfo();

$info->setShape( $myCircle );
$info->showInfo();
$info->setShape( $mySquare );
$info->showInfo();

$myRect = new Rectangle;
$myRect->setColor( "yellow" );
$myRect->fill();
$myRect->setWidth( 4 );
$myRect->setHeight( 5 );

$info->setShape( $myRect );
$info->showInfo();

?>

The code above generates the following result.