Unlike what many might imagine, pure and/or abstract class is basically the same element. This type of nomenclature is used to define classes that are not instantiated directly, they are designed exclusively for use in descendent classes.

oop.fw

To make it easier to understand the use of this concept, we present a practical example: the ‘person’ class has methods and attributes related to the person, but it is used only in the calling of other classes, for example, in ’employee’ and ‘manager’ classes. This concept helps in structuring the objects, thus ensuring that all controls can be manipulated and edited in an optimized and centralized way when these are common between two or more classes. In the PHP language, abstract classes are created as follows:

<?php
abstract class Person
{
abstract protected function typeSalary();
abstract protected function valueSalary( $prefix );
public function press () {
print $this-> typeSalary();
}
}

class Employee extends Person
{
protected function typeSalary{
return “Emplyee Salary”;
}

public function valueSalary( $prefix ) {
return “Salary Employe: “. $prefix;
}
}

class Manager extends Person
{
protected function typeSalary{
return “Manager Salary”;
}

public function valueSalary( $prefix ) {
return “Manager Salary: “. $prefix;
}
}

 

$class1 = new Employee;
$class1->press();
echo $class1-> valueSalary (2000)  ;

$class2 = new Manager;
$class2->press();
echo $class2-> valueSalary (5000) ;
?>

 

In the example, it is possible to understand the applicability of the concept. The abstract class “person” has a single common method (print) and two abstract methods (typeSalary and valueSalary). The abstract methods can be rewritten within the descendant; for this, the abstract statement must be inserted before the function statement in the abstract parent class methods. Only these ones can be rewritten in descendant classes. If the programmer attempts to rewrite a common method already implemented in the parent class, an error is displayed, stating that another method already exists with the same name.

When creating objects, only classes “Employee” and “Manager” receive instances. The class “person” is protected within the descendant classes. The common method of the parent class can be used by both objects; the abstract ones, in turn, need to be implemented in descendant classes, thus presenting a different behavior. This behavior then return results in the following screen:

Employee Salary

Employee Salary: 2000

Manager Salary

Manager Salary: 5000

 

By , The most efficient web development environment. Create your web solutions in a fast and innovative way, reducing the development time in up to 80%. Access http://scriptcase.net and learn more.

June 19, 2015

a

You might also like…

The importance of training in the use of software

Today we are going to reflect on the importance of training in the use of software that, when explo...

A sneak peek on scriptcase 9.4.027!

Come check out all the new details for scriptcase version 9.4.027 !! This week we launched our n...

8 tips for working from home

Hello, how are you? We hope you are well! We know that the moment is difficult and many people a...

You might also like…

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at sales@scriptcase.net. Learn more about our Privacy Police.