Welcome Guest, Not a member yet? Register   Sign In
PHP Interface, how and where?
#1

[eluser]Andreas Bergström[/eluser]
I want some of my controllers to implement a certain interface. Where do I put this interface code so that CI will find and load it? Done some searching without any luck.

Thanks for any help.
#2

[eluser]Sbioko[/eluser]
Codeigniter it is not a PHP5 framework. It does not work with interfaces and other PHP5 features. You need to do it by myself.
#3

[eluser]Jamie Rumbelow[/eluser]
Hey Andreas,

Unfortunately, CodeIgniter doesn't support PHP interfaces straight out the box (well, in the sense you want it to, anyway) - you'll have to create a file in your controllers directory which you then load. Either that or put it at the bottom of a MY_Controller base class file and extend from that Smile

It's a concept that's never really been written into a convention... not many CI users are hardcore PHP5 programmers and so rarely use interfaces.

Jamie
#4

[eluser]HdotNET[/eluser]
Hi Andreas,

Jamie is right. The more hardcore features of php5 are best used by extending CI to do what you want.

Then again, some people might argue whats the point?

H
#5

[eluser]cjbuchmann[/eluser]
Whats the point?

Interfaces are an integral part of OOP! They essentially force your classes to adhere to certain rules. For instance, If I have an awesome comparison algorithm that can compare multiple types of objects to one another, I may need them to have similar characteristics. For instance, I might expect all of these objects to have an ID. I might even write code that assumes this to be true, in which case it NEEDS to be true. Thus I would implement an interface with those classes, to ENSURE that they have an ID.

In my opinion, this is a very important feature, and I do hope that codeigniter chooses to incorporate this in future version (by default).
#6

[eluser]theprodigy[/eluser]
I may be wrong here, but if you would require all your classes to have an ID to make sure your algorithm works properly, then why not just add an ID to all your classes? If you know what each class needs, just add it. There's no point in adding an interface.

With that said, if you are adding some functionality that was written by someone else, and you are not sure of what all is necessary in order for it to work, then yes, maybe using an interface would be nice, as it would let you know if something is missing. But, on the other hand, documentation could do the same thing.
#7

[eluser]Jelmer[/eluser]
I've implemented using both abstract classes and interfaces by adding an autoloader and putting them in pre-defined directories (libraries/Abstracts & libraries/Interfaces) and giving them specific prefixes ("Ab_" and "Int_") to prevent unneccessary filesystem calls. I've posted my autoloader here.
Of course you don't have to use as many conventions as I do, but that's for you to decide.

My implementation allows stuff like this:
Code:
class Something extends Ab_baseclass implements Int_model, Int_model_plus {
    // code
}
(not very likely combination for me, but as an example)

And for those not getting the usefullness of interfaces: it prevents mistakes. Especially when working with a team and you're not sure everyone always reads you documentation. But also just for yourself when your applications are as modular as I tend to write mine. When your workload is heavy it's very easy to forget something that doesn't show imediately, and using the strict rules forced by implementing interfaces you can prevent many of those mistakes.
Also of course a good thing for testing the type of an object, you can ask if it's an implementation of a certain interface and after that be very sure how everything works you're using (or at least the input).
#8

[eluser]cjbuchmann[/eluser]
As Jelmer said, having interfaces helps prevent mistakes. Like many programming conventions it is not necessary in the sense that you're program won't run without it. But using interfaces are the difference between knowing something has the functionality you need, and assuming it has the functionality you need. If I'm coding, whether its by myself or in a team, I prefer to know that my classes have the functionality I need.

Also, as I had to get the interfaces loaded in, I came up with a solution similar to Jelmers. However, it seemed different enough to post here.

in autoload.php
Code:
$autoload['interfaces'] = array('RecieveInterface',
                                'TestInterface'
                                );

and then at the bottom, I made a loop to go through this and load in the classes, assuming they are stored in "applications/interfaces/".
Code:
foreach($autoload['interfaces'] as $interface)
{
    include_once(APPPATH."/interfaces/$interface.php");
}

Well I do consider this more of a hack, it didn't seem to bad, and I was still able to keep everything in the same location, and you load the interfaces the same way you would any other component (models/view/controllers). It is also nice, because I was looking for a way to load libraries without actually instantiating them, and I was able to use the same method.




Theme © iAndrew 2016 - Forum software by © MyBB