Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Some Notes on the Object-oriented Model of PHP

DZone's Guide to

Some Notes on the Object-oriented Model of PHP

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

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?



Source: http://www.stoimen.com/blog/2011/10/20/some-notes-on-the-object-oriented-model-of-php

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}