PHP integrates another concept of object-oriented programming: abstract classes. This course defines and introduces the concept of abstract classes. We will present what abstract classes, what they are used for development and how to declare and use them. Finally, we will study the particular case of final classes and methods involved in the security of the code object oriented programming.
We’ll be covering the following topics in this tutorial:
Presentation and reporting abstract classes
Defining abstract classes
Abstract classes enroll more in the safety of the object oriented programming. The first feature of an abstract class is that it can not be instantiated (and thus create an object). From this statement, we deduce logically that an abstract class is declared to be derived in concrete classes.
An abstract class acts like a typical concrete class. That is to say, it can declare attributes and traditional methods that will be accessible in derived classes. Depending on the chosen course visibility (public, private, and protected) for each of the attributes and methods.
Until then, there is no change from the concrete classes except the fact that we can not instantiate abstract classes. This is where the abstract methods. We will see later that a class declared abstract can also define abstract methods. The latter will have to be redefined in derived classes. It is a way of ensuring that the derived class will adopt the desired behavior.
Declaration of an abstract class
The declaration of an abstract class is realized using the keyword “abstract”. Take the example of an abstract class “HumanBeing” that will, for example, derived by two concrete classes “Male” and “Femal”.
Declaration of an abstract class
<?php
abstract class HumanBeing
{
protected $sex;
protected $name;
public function setName ($name)
{
$this-> name = $name;
}
public function getName ()
{
return $this-> name;
}
public function getSex()
{
return $this-> sex;
}
}
?>
If we attempt to instantiate this class to create an object of type HumanBeing, we will get the error message below:
Fatal error: Can not instantiate abstract class in HumanBeing /Users/Tutorial/abstract.php on line 35
We now focus on the other subtlety of abstract classes: the abstract methods.
Declaration and redefinition of abstract methods
An abstract method is declared using the keyword “abstract”. It is a partially defined in the class method. Indeed, when an abstract method is declared, it only defines its prototype (signature). Derived classes must necessarily redefine completely (body + prototype) all abstract methods of the parent class.
Returning to our previous example and add some abstract methods to our abstract class “HumanBeing”.
Declaration abstract methods
<?php
abstract class HumanBeing
{
protected $sex;
protected $name;
abstract function toSports();
abstract function toEntertain ();
public function setName ($name)
{
$this-> name = $name;
}
public function getName ()
{
return $this-> name;
}
public function getSex()
{
return $this-> sex;
}
}
?>
Our class now has two new abstract methods to toSport () and toEntertain (). As we can see, the bodies of these two methods is not defined. We will have to define in derived classes.
Note: Any class that defines one or more abstract methods must be declared abstract as well.
That is why we will now define two classes “Man” and “Woman” who will inherit both the properties and methods of our abstract class. Simply start with reporting and attempt to instantiate.
Declaration of derived classes
Declaration of derived classes Man and Woman
<? php
class Man extends HumanBeing
{
/ **
* Built Man object
*
*param String $ name Human Name
*return Void
* /
public function __construct ($ name)
{
$ this-> name = $ name;
$ this-> sex = 'M';
}
} class Female extends HumanBeing
{
/ **
* Built Object Woman
*
*param String $ name the woman Name
*return Void
* /
public function __construct ($ name)
{
$ this-> name = $ name;
$ this-> sex = 'F';
}
}
?>
Now try to instantiate one of two classes.
<?php
$oBob = new Man ('Bobby');
echo $oBob-> getName ();
?>
Of course, we get the following error because we have not redefined the abstract methods of the superclass.
Fatal error: Class Man contains two abstract methods and must be Declared Therefore abstract gold Implement the remaining methods (HumanBeing::toSport,
HumanBeing::toEntertain themselves) in /Users/Tutorial/abstract.php on line 74
This indicates that we must explicitly redefine the abstract methods of the superclass or make our abstract male class. Since we want to instantiate the derived class, it remains that the first solution. Thus redefining these two abstract classes in each of the derived classes.
Redefinition of abstract methods in derived classes
<?php
class Man extends HumanBeing
{
public function __construct ($name)
{
$this-> name = $ name;
$this-> sex = 'M';
}
public function toSport ()
{
echo $this-> name. ' made boxing ';
}
public function toEntertain ()
{
echo 'Evening football and beer';
}
}
class Female extends HumanBeing
{
public function__construct ($name)
{
$this-> name = $name;
$this-> sex = 'F';
}
public function toSport()
{
echo $ this-> name. ' makes fitness';
}
public function toEntertain ()
{
echo 'Shopping girls';
}
}
?>
Now instantiate each of the two classes.
Instances of classes Man and Woman
<? php
$ oAlice = new Woman ('Alice');
$ oAlice-> toSport ();
echo '<br/>';
$ oAlice-> toEntertain ();
echo '<br/>';
echo 'Sex', $ oAlice-> getSex ();
echo '<br/>';
$ oBob = new Man ('Bobby');
$ oBob-> toSport ();
echo '<br/>';
$ oBob-> toEntertain ();
echo '<br/>';
echo 'Sex', $ oBob-> getSex ();
?>
The result is the one expected:
Alice makes fitness
Shopping girls
Sex: F
Bobby makes boxing
Evening football and beer
Gender: M
For instances of Women class, we find good value for F attribute $sex and redefined the abstract methods that correctly displays the fitness and shopping. As for instance the Man class, the result is as expected. The attribute $sex takes good value M and the two abstract methods show well the Boxing Sport and Football Evening entertainment.
Special cases of final classes and methods
Presentation of final classes and methods
So far we have presented the abstract classes and methods. PHP introduces an additional mechanism to ensure the programming of safety. This is the keyword “final” that may be applied either to a class or a method.
When a class is defined as “final”, it means that it can be derived by a subclass. This also implies that its attributes and methods may not be redefined. However, if we apply the keyword “final” a method of a class, then it is only this method can not be overridden in derived classes.
By prohibiting the derivation of a class or redefine (overload) methods of a class, this allows you to ensure that the developer does not directly bypass the logic you have implemented.
Declaration of final classes
Let our 3 previous classes. Now we decide that classes “Man” and “Woman” can not be derived. We therefore declare as final. This gives us:
Declaration of final classes
<? php
final class Man extends HumanBeing
{
// Following the class code
}
final class Female extends HumanBeing
{
// Following the class code
}
?>
Now trying to derive one of the two classes and instantiated. For this, we need only write a class “YoungBoy” which inherits the properties and methods of the class “Man”.
<? php
class YoungBoy extends Man
{
/ **
* Built the object Youngboy
*
*param String $ name name of the young boy
*return Void
* /
public function __construct ($ name)
{
parent :: __ construct ($ name);
}
}
?>
Obviously an error is generated because the Man class is no longer differentiable because of his statement “final”.
Fatal error: Class Youngboy May not inherit from final class (Men) in /UsersTutorial/abstract.php on line 153
Note: An abstract class may not be declared “final”. A syntax error will be generated. In addition, declare an abstract and final class is synonymous with a lack of logic since the purpose of an abstract class is to be derived by subclasses.
Final Declaration methods
Let us now study the case of final methods. Let the abstract class “HumanBeing” and declare methods “getName ()” and “getSex ()” final.
Final Declaration methods
<?php
abstract class HumanBeing
{
public final function getName ()
{
return $this-> name;
}
public final function getSex ()
{
return $this-> sex;
}
}
?>
Now derived classes “Man” and “Woman” can not redefine these methods. Try to override one of these methods in the “Man” class.
Redefining abstract methods
<?php
final class Man extends HumanBeing
{
public function getSex ()
{
return 'Male';
}
}
?>
Here, the method “getSex ()” no longer returns the value of $ sex protected attribute, but attempts to return the string “male”. Not surprisingly, redefining the method generates a fatal error by the PHP interpreter.
Fatal error: Can not override final method humanbeing :: getSex () in /Users/Tutorial/abstract.php on line 115