PHP Classes

What is Class Abstraction in PHP?

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog What is Class Abstrac...   Post a comment Post a comment   See comments See comments (4)   Trackbacks (0)  

Author:

Viewers: 2,360

Last month viewers: 194

Categories: PHP Tutorials

When you are ready to share your code for other class developers to use, you will need to control how that code is used and provide some way to enable updates.

Read this tutorial to learn how to use class abstraction with closely related objects to provide greater control over your PHP code, so it facilitates future updates.




Loaded Article

Contents

Introduction

What is Class Abstraction?

A Few Important Things to Remember

Abstraction as a Template

Concrete Classes

Abstraction as a Partial Implementation

Can Class Properties be Abstract?

Conclusion


Introduction

This tutorial assumes you have a working knowledge of class inheritance, visibility and instantiation. You may review these concepts in my previous article. Please note that the examples in this tutorial are not complete models and are provided to illustrate a concept.

There are going to be times when you want to build a class that has some control over how other classes built around it are designed. It may be necessary that a specific method be implemented to receive specific data. You may want to provide class updates without forcing other classes to be changed. We can achieve this through class abstraction.

What is Class Abstraction?

Class abstraction is a way for a class to force any other class that extends it to implement specific methods. The parent class is called an abstract class, the child class we will call a concrete class, and the methods a concrete class must implement are called abstract methods.

For our purposes, abstract is a generalized concept and concrete is a specific implementation of the abstract.

A Few Important Things to Remember

The first thing to remember is that any class that has at least one abstract method, is an abstract class.

The second thing to remember is that an abstract class can not be instantiated, you must instantiate the concrete class.

The third thing to remember is that abstract methods do not implement the method, they only provide the abstract for the concrete class to implement.

The following examples will illustrate how these things come together.

Abstraction as a Template

The first type of abstract classes we will call templates. A template only contains abstract methods. An abstract class as a template is useful when you want to set specific expectations in any concrete class, ensuring a uniform model.

Here is a simple example of an abstract class as a template.

abstract class theBoss {

abstract protected function showWork($project);

}

As we can see, this is an abstract class named theBoss and contains one abstract method named showWork. The showWork method has one property, $project which is called the method signature.

You can read this as, I am theBoss and you will implement a method named showWork and that method must accept project data. TheBoss does not care how it is implemented, it only cares that it is implemented.

You may add as many abstract methods to a class as you need. Just remember that they are not implemented in the abstract class, only providing the name and method signature for the concrete class to implement.

Concrete Classes

Now let us extend theBoss with a concrete class.

class theEmployee extends theBoss {

public function showWork( $project = 'Nothing' ) {

$work = 'I am working on '.$project;

return $work;

}

public function startWork($project) {

return $this->showWork($project);

}

}

Here we have theEmployee class which extends theBoss. It has done its job and implemented the showWork method with a matching method signature.

Take a moment and look back at the abstract method and see that its visibility is protected, while the implemented method has a visibility of public. The implemented method can change the visibility as long as it is less restrictive than the abstract, so in this case public is allowed where private would not be.

TheEmployee also provides its own method, startWork. A concrete class can implement as many methods as it needs as long as it implements the abstract methods it is required to.

Now let us extend theEmployee with a concrete class.

class theContractor extends theEmployee {

public function showWork( $project, $employeeName='' ) {

$work = empty($employeeName) ? 'I am working on ' . $project : $employeeName . ' has me working on '.$project;

return $work;

}

Here we have theContractor class which extends theEmployee which extended theBoss. It has done its job also and implemented the showWork method with a matching signature.

Wait a minute, its properties also contain $employeeName, so do the method signatures really match? They do, an implemented method can include additional properties as long as those properties are optional. $employeeName is optional since it is initialized with a default value, an empty string in this case.

Notice also that theContractor implements the showWork method differently than theEmployee. Once again, theBoss does not care how it is implemented, only that it is implemented. Also note that because of inheritance, theContractor also has access to the startWork method from theEmployee.

You can instantiate either theEmployee class or theContractor class since they are concrete classes, just not theBoss class because it is an abstract class.

Abstraction as a Partial Implementation

There is a second type of an abstract class which we will call a partial implementation. A partial implementation contains fully implemented methods, called common methods, along with abstract methods. An abstract class as a partial implementation can be far more useful than just a template.

Here is the boss again as a partial implementation.

abstract class theBoss {

abstract protected function showWork( $project );

final public function assignWork( $employee, $project) {

$assigned = $employee . ' will be working on ' . $project;

return $assigned;

}

}

As you can see, theBoss has now implemented a common method, assignWork, which is inherited by all concrete classes. It also has added the final keyword to the method so that no concrete class can implement it differently, it is theBoss after all.

A common approach is to use common methods in the partial implementation to provide high end functionality to concrete classes and then require the concrete classes to implement specific methods for processing the data.

In other words we all should be able to understand, theBoss is responsible for making important decisions and making sure theEmployee and theContractor are prepared to receive instructions so that they can perform the labor.

Another advantage to partial implementation is versioning. You can update a common method and these updates will automatically be applied to all concrete classes without having to make any changes to the concrete class.

Using theBoss as an example, lets say that theBoss decides the sentence, [employee] will be working on project [project], does not get the point across, so it is changed to, [employee] must work on the [project] project. All instances calling the assignWork method will now display the 'improved' directive.

Can Class Properties be Abstract?

The simple answer is no. There is no need for a class property to be abstract. Class properties and constants are not implemented, instead they are declared, which in a way already makes them abstract. They follow the same rules and are treated the same way when declared in an abstract class as they would any other class.

Conclusion

Class abstraction is useful whenever you want to provide common implementation across similar objects. It is the road map to how these similar objects are created so they can make use of any common methods while providing a flexibility to implement an abstract method that best suits the needs of the object.

Post a comment if you liked this article or have questions about class abstraction.




You need to be a registered user or login to post a comment

1,616,722 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

4. Clear and concise. - Martin Latter (2015-06-09 20:31)
-... - 0 replies
Read the whole comment and replies

3. Shipping Proprietary Code - Cyril Ogana (2015-06-08 05:46)
We ship our proprietary classes as abstract but Ioncube encoded... - 0 replies
Read the whole comment and replies

2. Abstraction is needed - Thiago Arcanjo (2015-06-01 20:16)
we need implement ouro Codés with abstract classes... - 0 replies
Read the whole comment and replies

1. Great example! - Ricardo Aponte (2015-06-01 20:15)
Excellent Post... - 0 replies
Read the whole comment and replies



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog What is Class Abstrac...   Post a comment Post a comment   See comments See comments (4)   Trackbacks (0)