<? echo

Project Statuses

Mental Fusion - 85%
FusionBB - 2%
This webpage is built with W3C compliant HTML5.
This webpage is built with W3C compliant CSS3.
Download: Fast, Fun, Awesome
Opera, the fastest and most secure web browser

Example Information

Object Interfaces

Posted: April 22, 2012 2:05 PM CDT by Michael
With this article, I hope to answer the following questions: What is an Object Interface? How do you create an Object Interface? How do you use an Object Interface and what issues could you run into when using one? Why use an Object Interface?

Hopefully I'll answer each question sufficiently enough that I don't create more questions than answers, but we'll see. I expect this article to be on the short side, but (again) we'll see.

To start with: What is an Object Interface?

An Object Interface is a tool that allows you to create skeletons of classes, per se. Interfaces are similar to Abstract Classes in that any method declared within the Interface must be declared by the implementing class. Interfaces differ from Abstract Classes in that they cannot have their own properties and methods defined (e.g., you cannot create the method helloWorld() within an interface and have it print, "Hello, world!"). The implementing class must define any method specified by the Interface. Further, all methods must be declared public within the Interface and implementing class.

Note: Any class can implement multiple interfaces (using the comma operator) as long as the interfaces do not have overlapping (same-named) methods. Also, interfaces may have their own constants (const) defined which cannot be redefined by the implementing class. Interfaces can also be extended by using the extends operator.

How do you create an Object Interface?

Creating an Object Interface is pretty simple. You just need to define it using the interface operator; beyond that, it's similar to an Abstract Class. For this example, let's call our Interface "Testerface" to be punny:

// Define the Testerface interface
interface Testerface {
// helloWorld() method
public function helloWorld();

// goodbyeWorld() method
public function goodbyeWorld();

// add() method
public function add($num1,$num2);
}

That's fairly straight forward, I hope. As you can see, each method is defined as public because you cannot make an Interface method protected or private. For the sake of simplicity, you could also leave the public declaration off and the methods will be public by default, but I added it here for clarity.

How do you use an Object Interface and issues could you run into when using one?

To use an Object Interface, you need to create your class and use the implements operator to specify which interface to use. Let's create a simple Tester class to demonstrate:

// Tester implements the Testerface interface:
class Tester implements Testerface {

}

Above, you can see Tester is declared as implementing the Testerface interface we set up earlier. So what happens if we try to instantiate the Tester class as-is? Since Tester doesn't redefined the methods listed in Testerface, we'll run into an error like the following:


PHP Fatal error: Class tester contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (Testerface::helloWorld, Testerface::goodbyeWorld, Testerface::add)


Now, this can be confusing because it says the methods in the Interface are astract methods and can be defined as such, but they cannot be defined as abstract within the Interface. Like visibility, abstraction must be defined in the implementing class, not the Interface.

To fix this error, let's redefine all of the methods listed in the error:

// Tester implements the Testerface interface:
class Tester implements Testerface {

// helloWorld() method
public function helloWorld() {
echo "Hello, world!";
}

// goodbyeWorld() method
public function goodbyeWorld() {
echo "Goodbye, world!";
}

// add() function, missing params
public function add() {
echo 1+1;
}

}

Now all of our methods are defined, but one of them (add()) is missing the parameters the Interface declared. When this happens, the follow error is thrown:


PHP Fatal error: Declaration of Tester::add() must be compatible with that of Testerface::add()


To find out what the parameters are, you can use the Reflection object (PHP 5) to export the information for the interface to determine what the parameters should be (alternatively, you can look it up manually in the file for the Interface, but this is cooler):

// Use Reflection to export the information about the Testerface interface:
Reflection::export(new ReflectionClass('Testerface'));

The above will output the following:

Interface [ <user> interface Testerface ] {
@@ /usr/local/www/mentalfusion.org/interface.php 2-6

- Constants [0] {
}

- Static properties [0] {
}

- Static methods [0] {
}

- Properties [0] {
}

- Methods [3] {
Method [ <user> abstract public method helloWorld ] {
@@ /usr/local/www/mentalfusion.org/interface.php 3 - 3
}

Method [ <user> abstract public method goodbyeWorld ] {
@@ /usr/local/www/mentalfusion.org/interface.php 4 - 4
}

Method [ <user> abstract public method add ] {
@@ /usr/local/www/mentalfusion.org/interface.php 5 - 5

- Parameters [2] {
Parameter #0 [ <required> $num1 ]
Parameter #1 [ <required> $num2 ]
}
}
}
}

You can read all about the Reflection class here, but the important information we wanted was the parameter list for the method add(). Scanning the list of info, we can see our add() method's information at the bottom:

Method [ <user> abstract public method add ] {
@@ /usr/local/www/mentalfusion.org/interface.php 5 - 5

- Parameters [2] {
Parameter #0 [ <required> $num1 ]
Parameter #1 [ <required> $num2 ]
}
}

Here, we can see it has 2 parameters: $num1 and $num2. So let's add these to our class's method definition for add() and see what happens:

public function add($num1, $num2) {
echo ($num1+$num2)."
";
}

Unless something else has erred, our file should now run error-free and we can instatiate the class and call its functions:

// instantiate Tester class:
$test = new Tester;

// outputs "Hello, world!"
$test->helloWorld();

// outputs "Goodbye, world!"
$test->goodbyeWorld();

// outputs the sum of $num1 and $num2
$test->add(18,24); // outputs 42

If no errors were encountered, congrats! You now have an object that is successfully implementing an Object Interface.

Why use an Object Interface?

This is a bit subjective. Nowadays, OOP is all about code-reusability, scalability, and ease of implementation. With interfaces, it's very simple to declare a fixed set of rules for any new class that you're going to develop. Each programmer likes to add his or her own personal flare to their code and interfaces help keep the code base structured and coder peculiarities in check.

Well this wasn't short at all.
; ?>