Some Notes on the Object-oriented Model of PHP

PHP 5 introduces interfaces and abstract classes. To become a little clearer, let us see their definitions.

Interfaces

Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.
Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.
All methods declared in an interface must be public, this is the nature of an interface.

To implement an interface, the implements operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal error. Classes may implement more than one interface if desired by separating each interface with a comma.

Abstract Classes

PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be instantiated, and 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. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. Furthermore the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same. This also applies to constructors as of PHP 5.4. Before 5.4 constructor signatures could differ.

Some Cases

Now let’s do a quick experiment. According to the definition of interfaces we can define an interface and than an abstract class can implement this interface.

interface A
{
	public function a();
}
 
abstract class B implements A
{
	public function a()
	{
		return 10;
	}
}

However the abstract class cannot be instantiated so we need to extend it.

interface A
{
	public function a();
}
 
abstract class B implements A
{
	public function a()
	{
		return 10;
	}
}
 
class C extends B
{
	...
}

In the first place is completely OK to define an abstract class that does not contain any abstract method. TUsually if a class has one or more abstract methods (Methods defined as abstract simply declare the method’s signature – they cannot define the implementation, according to the docs), it must be defined as abstract. But we can also define an abstract class which has no abstract methods.

abstract class B
{
	public function a()
	{
		return 5;
	}
}
 
class C extends B
{
	public function a()
	{
		return 10;
	}
}
 
$c = new C();
echo $c->a();

OK then what would happen if we had an interface that is implemented by an abstract class, which in turn is extended by a given class. First of all if a class implements an interface, even if it is an abstract class, it must implement all methods defined in the interface. So if the predefined method in the abstract class is marked as abstract, it would result into a fatal error.

interface A
{
	public function a();
}
 
abstract class B implements A
{
	abstract public function a();
}
 
class C extends B
{
	public function a()
	{
		return 5;
	}
}
 
$c = new C();
echo $c->a();

This is perfectly logical, because in fact each method of the interface must be implemented by his children.

“All methods in the interface must be implemented within a class; failure to do so will result in a fatal error.”

Fine, but we can have abstract classes with no abstract methods, let’s try this:

interface A
{
	public function a();
}
 
abstract class B implements A
{
	public function a()
	{
		return 10;
	}
}
 
class C extends B
{
	public function a()
	{
		return 5;
	}
}
 
$c = new C();
echo $c->a();

This code is working and the result is the returned value from C::a(). However class B is abstract so let’s go even further. Let’s define an abstract method, different from the method B::a().

interface A
{
	public function a();
}
 
abstract class B implements A
{
	public function a()
	{
		return 10;
	}
 
	abstract public function b();
}
 
class C extends B
{
	public function a()
	{
		return 5;
	}
 
	public function b()
	{
		return "hello world!";
	}
}
 
$c = new C();
echo $c->a();
echo $c->b();

Now there’s a fatal error. Why?

5 thoughts on “Some Notes on the Object-oriented Model of PHP

  1. Hello there!!

    I guess that the function name b colides with abstract class B name. If you change function b to w everything works as aspected.

    Very good resume about abstract and interfaces!!!
    keep going

  2. I agree with Sergio… Method is named b(), which is in that case resolved as the constructor, because class is also named B. In PHP, functions and methods names are NOT case-sensitive. As a result, in that example, you’ve made constructor of a B class abstract, and that’s the reason why C class can not be instantiated. Fatal error is triggered by this line: $c = new C(), and not those method calls.

Leave a Reply

Your email address will not be published. Required fields are marked *