Webhosting and cheap domain registration services
  

 Home

PHP Manual
PrevChapter 18. Classes and Objects (PHP 5)Next

Object Abstraction

PHP 5 introduces abstract classes and methods. It is not allowed to create an instance of a class that has been defined as abstract. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature they cannot define the implementation.

The class that implements the abstract method must define with the same visibillity or weaker. If the abstract method is defined as protected, the function implementation must be defined as either protected or public.

Example 18-14. Abstract class example

<?php

abstract
class AbstractClass {

   
/* Force Extending class to define this method */
   
abstract protected function getValue();

   
/* Common method */
   
public function print() {
     print
$this->getValue();
   }

}

class
ConcreteClass1 extends AbstractClass {

   
protected function getValue() {
     return
"ConcreteClass1";
   }

}

class
ConcreteClass2 extends AbstractClass {

   
protected function getValue() {
     return
"ConcreteClass2";
   }

}

$class1 = new ConcreteClass1;
$class1->print();

$class2 = new ConcreteClass2;
$class2->print();
?>

Old code that has no user-defined classes or functions named 'abstract' should run without modifications.


PrevHomeNext
Object ConstantsUpObject Interfaces

 

  

 

 

 Network sites:

Domain registration : Domain registration and domain name transfer services
Cheap Domain Registrar : Cheap domains or register domain name from $5.95
 

Disclaimer: This documentation is provided only for the benefits of our hosting customers.
For authoritative source of the documentation, please refer to http://www.php.net/docs.php