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 ); 
			}   
	      

	      		<?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:


Example

	      	<?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();

	      	?>
	      


Example

	      	<?php
	      		abstract class AbstractClass
				{
				    // Force Extending class to define this method
				    abstract protected function getValue();
				    abstract protected function prefixValue($prefix);

				    // Common method
				    public function printOut() {
				        print $this->getValue() . "\n";
				    }
				}

				class ConcreteClass1 extends AbstractClass
				{
				    protected function getValue() {
				        return "ConcreteClass1";
				    }

				    public function prefixValue($prefix) {
				        return "{$prefix}ConcreteClass1";
				    }
				}

				class ConcreteClass2 extends AbstractClass
				{
				    public function getValue() {
				        return "ConcreteClass2";
				    }

				    public function prefixValue($prefix) {
				        return "{$prefix}ConcreteClass2";
				    }
				}

				$class1 = new ConcreteClass1;
				$class1->printOut();
				echo $class1->prefixValue('FOO_') ."\n";

				$class2 = new ConcreteClass2;
				$class2->printOut();
				echo $class2->prefixValue('FOO_') ."\n";
	      	?>
	      

Example

	      	<?php
	      		abstract class AbstractClass
				{
				    // Our abstract method only needs to define the required arguments
				    abstract protected function prefixName($name);

				}

				class ConcreteClass extends AbstractClass
				{

				    // Our child class may define optional arguments not in the parent's signature
				    public function prefixName($name, $separator = ".") {
				        if ($name == "Pacman") {
				            $prefix = "Mr";
				        } elseif ($name == "Pacwoman") {
				            $prefix = "Mrs";
				        } else {
				            $prefix = "";
				        }
				        return "{$prefix}{$separator} {$name}";
				    }
				}

				$class = new ConcreteClass;
				echo $class->prefixName("Pacman"), "\n";
				echo $class->prefixName("Pacwoman"), "\n";
	      	?>
	      

Example

	      	<?php
	      		class Fruit { 
			    private $color; 
			    
			    public function eat() { 
			        //chew 
			    } 
			    
			    public function setColor($c) { 
			        $this->color = $c; 
			    } 
			} 

			class Apple extends Fruit { 
			    public function eat() { 
			        //chew until core 
			    } 
			} 

			class Orange extends Fruit { 
			    public function eat() { 
			        //peel 
			        //chew 
			    } 
			} 
	      	?>
	      

Example

	      	<?php
	      		abstract class Fruit { 
				    private $color; 
				    
				    abstract public function eat(); 
				    
				    public function setColor($c) { 
				        $this->color = $c; 
				    } 
				} 
	      	?>
	      




PHP Tutorials


Home

Introduction

Syntax

Variable

Comment

Echo and Print Statement

Data Types

Boolean

Integer

Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion