|
PHP 5 Abstract Classes And MethodsPosted: Jan 26, 2012 Last modified: Apr 27, 2018This article takes a look at PHP5's Object Oriented Programming (OOP) support for abstraction. Though it's easily possible to script in PHP without paying much attention to this powerful feature, larger projects will benefit from the increased class organization and structure that can be obtained as a result of careful usage of abstract classes and methods. This article assumes you have some knowledge of PHP OOP prgramming such as was available in PHP4, and in particular that you understand the concept of inheritance. An abstract class is a class that has not been fully implemented, and which will usually be used as a base class for other classes to inherit from and complete the implementation. The usefulness of this is that with an abstract class you define the interface all subclasses must have, whilst preventing anyone from instantiating a particular instance of the abstract class. A key advantage of an abstract class in comparison to an interface is that you can add the common elements of implementation that will be shared by the subclasses, and not just specify the interface of the subclasses. PHP 5 supports abstract classes and methods in a similar way to Java. The first thing to look at is how to create an abstract class by using the <?php
abstract class MyAbstractClass {}
This class has no body, so it's not a useful class that does anything, nor does it define an interface for subclasses to inherit, but it is still a legal class. As mentioned above, you cannot instantiate an abstract class, as executing the following program shows: <?php
abstract class MyAbstractClass {}
$obj = new MyAbstractClass();
/*
output:
Fatal error: Cannot instantiate abstract class MyAbstractClass in /path/to/test.php on line 3
*/
An abstract class can be partially (or even fully) implemented, so it is fine for it to contain normal methods: <?php
abstract class MyAbstractClass {
public function doSomething() {
echo "MyAbstractClass::doSomething()\n";
}
}
In an abstract class, you can also declare a method as abstract, which means that the method is not implemented and that any non-abstract subclass must implement it. An abstract method has no body: <?php
abstract class MyAbstractClass {
public abstract function doSomething();
}
Remember that an abstract class need not contain any abstract methods (the first class displayed above, for example, had no body at all), though in practice they usually will. On the other hand, if a class is not declared as abstract, it cannot contain any abstract methods: <?php
class MyClass {
public abstract function doSomething();
}
/*
output:
Fatal error: Class MyClass contains 1 abstract method and must therefore be declared abstract or implement the
remaining methods (MyClass::doSomething) in /path/to/test.php on line 4
*/
You can create a new abstract class that extends an earlier one (note how you do not have to implement the method body of <?php
abstract class MyAbstractBaseClass {
public abstract function doSomething();
}
abstract class MyAbstractSubClass extends MyAbstractBaseClass {}
So now that we've covered some of the rules concerning how to declare abstract classes and methods, we will look at a simple program that makes use of them: <?php
abstract class Vehicle {
public abstract function getNumWheels();
public function getName() {
return get_class($this);
}
}
class Car extends Vehicle {
public function getNumWheels() {
return 4;
}
}
class Bike extends Vehicle {
public function getNumWheels() {
return 2;
}
}
function printNumWheels(Vehicle $v) {
echo "A " . $v->getName() . " has " . $v->getNumWheels() . " wheels\n";
}
$car = new Car();
$bike = new Bike();
printNumWheels($car);
printNumWheels($bike);
/*
output:
A Car has 4 wheels
A Bike has 2 wheels
*/
The program starts by creating an abstract class There are a few things to note here. Firstly, Well that's all for now. I hope this article has helped you to understand how to use abstract classes and methods in PHP5 a little better! |