<? echo

Project Statuses

Mental Fusion - 85%
FusionBB - 2%
This webpage is built with W3C compliant HTML5.
This webpage is built with W3C compliant CSS3.
Download: Fast, Fun, Awesome
Opera, the fastest and most secure web browser

Example Information

Class Abstraction

Posted: April 22, 2012 3:41 PM CDT by Michael
For this article, I'll try my best to answer the following questions without being overly detailed and boring: What is Class Abstraction? How does an Abstract Class differ from an Object Interface? How can I use Class Abstraction?

I won't go into a terrible amount of detail for this one as Class Abstraction, from my experience, is simply a combination of PHP5's Object Interfaces (with some differences) and a normal class.

What is Class Abstraction?

Class Abstraction is a fancy way of saying "any methods within this class marked as abstract must be redefined by any extending class."

How does an Abstract Class differ from an Object Interface?

I'll list out what an Abstract Class can do that an Interface cannot do to try and make things simpler. Abstract Classes can:

  • Have properties.
  • Specify public or protected Visibility for abstracted methods.
  • Contain common properties and methods which can be used by the extending class(es).
  • Implement interfaces.

Like Interfaces, Abstract Classes cannot be instantiated by themselves without receiving a fatal error. You must extend the class to use it. Let's set up a test class called AbstractClass for this example:

// AbstractClass definition
abstract class AbstractClass {
// common property usable by any extending class.
protected $commonVariable = "Greetings.";

// force extending class to redefined hellowWorld()
public abstract function helloWorld();

// protected function goodbyeWorld(); must be redefined
protected abstract function goodbyeWorld();

// common function usable by any extending class without needing to be redefined.
public function commonFunction() {
echo "I'm common.";
}
}

In order to use this class, we must create a new class that extends it. Let's create one now called ExtendedClass:

// ExtendedClass definition
class ExtendedClass extends AbstractClass {
// test function to see if the class works:
public function test() {
echo 'It works!';
}
}

Without redefining the 2 methods specified as abstract in AbstractClass we will receive the following error:


PHP Fatal error: Class ExtendedClass contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (AbstractClass::helloWorld, AbstractClass::goodbyeWorld)


This is the exact same issue we encounter with Interfaces that do not have their methods defined properly in an implementing class. To fix the error, redefine the methods (their signatures (arguments/parameters) must match the abstract method definition in the abstract class):

// ExtendedClass definition
class ExtendedClass extends AbstractClass {
// test function to see if the class works:
public function test() {
echo 'It works!';
}

// redefined helloWorld()
public function helloWorld() {
echo $this->commonVariable.", world!";
}

// redefined goodbyeWorld() with lesser visibility restriction
public function goodbyeWorld() {
echo "Goodbye, world!";
}
}

Notice that we redefined goodbyeWorld() with a lesser visibility restriction than the abstract class. You don't have to change the visibility restriction, but it's important to note that you can as long as the visibility is equal to or less strict than the abstract class's visibility definition for the method (e.g., you cannot make a method private if it is public or protected in the abstract class, but you can make a method public if it is protected in the abstract class).

Note that we also use the common parameter commonVariable within our redefined helloWorld() function.

Now we can test our class:
// Instantiate our class:
$test = new ExtendedClass;

// outputs "Greetings, world!"
$test->helloWorld();

// outputs "It works!"
$test->test();

// outputs "I'm common."
$test->commonFunction();

// outputs "Goodbye, world!"
$test->goodbyeWorld();


How can I use Class Abstraction?

This is another question whose answer is fairly subjective. Like Object Interfaces, Class Abstraction is used to streamline and organize code so that your code base doesn't look like 20-30 different programmers have been in it in the last month (even if that's actually the case). However, unlike Object Interfaces, Class Abstraction also provides the ability to integrate utility functions and parameters which all exnteding classes inherit. Abstract classes are very flexible, but they are a little more robust than Object Interfaces and if you find yourself abstracting the same functions over and over, you ought to consider placing those functions in an Object Interface instead to save memory, space, and processing.
; ?>