Php why to use abstract classes
|
A simple example This is a very simple way of explaining abstract classes. Lets say we have the following code: <?php class Fruit { private $color; public function eat() { // chew } public function setColor($c) { $this->color = $c; } } class Apple extends Fruit { public function eat() { // chew until core } } class Orange extends Fruit { public function eat() { // peeling // chew } } ?> Now I give you an apple and you eat it. What does it taste like? It tastes like an apple. <?php $apple = new Apple(); $apple->eat(); ?> Now I give you a fruit. <?php $fruit = new Fruit(); $fruit->eat(); ?> What does that taste like? Well, it doesn't make much sense, so you shouldn't be able to do that. This is accomplished by making the Fruit class abstract as well as the eat method inside of it. <?php abstract class Fruit { private $color; abstract public function eat() public function setColor($c) { $this->color = $c; } } ?> An abstract class is just like an interface, but you can define methods in an abstract class whereas in an interface they are all abstract. Abstract class can have both empty and working/concrete methods. In interfaces, functions defined there cannot have a body. In abstract class it can. A real world example <?php abstract class person { public $LastName; public $FirstName; public $BirthDate; abstract protected function write_info(); } final class employee extends person{ public $EmployeeNumber; public $DateHired; public function write_info(){ //sql codes here echo "Writing ". $this->LastName . "'s info to emloyee dbase table <br>"; } } final class student extends person{ public $StudentNumber; public $CourseName; public function write_info(){ //sql codes here echo "Writing ". $this->LastName . "'s info to student dbase table <br>"; } } ///---------- $personA = new employee; $personB = new student; $personA->FirstName="Joe"; $personA->LastName="Sbody"; $personB->FirstName="Ben"; $personB->LastName="Dover"; $personA->write_info(); // Writing Sbody's info to emloyee dbase table $personB->write_info(); // Writing Dover's info to student dbase table ?> 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. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child. Additionally, these methods must be defined with the same (or a less restricted) visibility. Again An interface is always an agreement or a promise. When a class says "I implement interface Y", it is saying "I promise to have the same public methods that any object with interface Y has". On the other hand, an Abstract Class is like a partially built class. It is much like a document with blanks to fill in. It might be using English, but that isn't as important as the fact that some of the document is already written. An abstract class is the foundation for another object. When a class says "I extend abstract class Y", it is saying "I use some methods or properties already defined in this other class named Y". <?php class X implements Y { } // this is saying that "X" agrees to speak language "Y" with your code. class X extends Y { } // this is saying that "X" is going to complete the partial class "Y". ?> |