Jump to content

Interfaces


blacksmoke90

Recommended Posts

Good evening

 

I hope someone would be so kind as to help me.

 

I am completely new too php oop and I just need help with one tiny problem.

I have written some code and I understand most of the concepts concerning oop in php, it's just that I am struggling with interfaces and I don't really see why it should be used if I can just create methods, but anyway I was told it is a good practice to use interfaces because one of the main reasons of oop is to re-use code, So I have implemented an interface but I can call the method with and without the implementation of the interface so I was kind of wondering, what the purpose of interfaces are? or am I doing something wrong?

Link to comment
Share on other sites

An interface is typically used by framework and library creators.

 

It facilitates certain types of design patterns, for example the factory pattern or for use in dependency injection situations.   What an interface guarantees is that *your* class will be sure to include some number of methods that another class can call.

 

For example, let's say that I have a "shapeMaker" class that instantiates a graphic canvas and then adds and removes objects of different shapes to it.  It might have a method like this:

class ShapeMaker($canvas) {
    private $shapes = array();
    private $canvas;
    .....   
    
    function addShape($shapeObj) {
        $this->shapes[] = $shapeObj;
        $shapeObj->draw($this->canvas);
   }
}

The designer of the framework will very probably include an interface for people to create new shape classes, which include the draw($shapeObj) method signature.  By providing that interface, they guarantee that any object that is passed into the shapeMaker class via the addShape, can then immediately have it's draw($canvas) method called.

 

The details of exactly how draw works are left to the class developer, but the interface helps establish the contract of methods needed by the factory class in order to manipulate any object that implements an interface.

 

I rarely see the use of interfaces outside of a framework or library, but there is no reason you can't use them yourself if you have a solid grasp of what they are for.  Usually people are utilizing inheritance rather than interfaces, but in complicated class dependency situations, it's good to keep in mind that you can write a class that can implement 2 or 3 different interfaces, and still inherit from a base class. 

 

In PHP, you only have single inheritance, so interfaces were a way to get around that limitation in some circumstances, although in php5.4 they added traits which are another way of getting around single inheritance in many cases.

Link to comment
Share on other sites

Another benefit to using interfaces is that you can type hint to the interface, like so:

interface iMyInterface{
	function sayHello();
}

class TestOne implements iMyInterface{
	function sayHello(){
		return '<p>Good day!</p>';
	}
}

class testTwo implements iMyInterface{
	function sayHello(){
		return "<p>'sup?</p>";
	}
}

class EverybodyPolite{
	public static function greet(iMyInterface $greeting){
		print($greeting->sayHello());
	}
}

$p1 = new TestOne();
$p2 = new TestTwo();

EverybodyPolite::greet($p1);
EverybodyPolite::greet($p2);

While you can drop the type hint from the public static greet() method, having it there increases readability of the code and the possibilities that the code will function as expected because you know that the object passed in to the calling method functions (at it's base) in a defined way.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.