开发者

Using PHP interfaces in Codeigniter

I am trying to find out how can I used PHP interfaces in my MVC design. I want to make sure that the design enforces an interface so that any new module would follow that.

For example:

<?php

interface BaseAPI {
     public function postMessage($msg);
}

class ServiceAPI implements BaseAPI {
     public function postMessage($msg) { return $msg; }
}

class Service_Two_API implements BaseAPI {
     public function postMessag开发者_如何学Ce($msg) { return "can't do this: ".$msg; }
}

?>

I want to do this in CI. Is it possible? how should I design it?


Here's How to Get CodeIgniter to Load Interfaces Properly

In your application/config/autoload.php:

// Add "interface_autoloader" to your models array
$autoload['model'] = array('interface_autoloader');

Now create a new class in your application/models folder "interface_autoloader.php":

<?php

class Interface_autoloader {

    public function __construct() {
        $this->init_autoloader();
    }

    private function init_autoloader(){
        spl_autoload_register(function($classname){
            if( strpos($classname,'interface') !== false ){
                strtolower($classname);
                require('application/interfaces/'.$classname.'.php');
            }
        });
    }

}

Now create a new folder in your application folder called "interfaces":

Using PHP interfaces in Codeigniter

Then just add your interfaces into the "interfaces" folder and you should be able to use them like normal.


I'm using interfaces in my codeigniter project. I just do it:

Some classes need to extends a personal Controller, so I have created the librarie called Module_Controller extends Controller. This library is autoloaded.

In the same file, I have been declared the interface. So, my file libraries/Module_Controller.php has the following code:

class Module_Controller extends Controller{
...
}

interface modular{
...
}

In this way, when this file be loaded, the interface will be declared for everyone.


You can create MY_Loader class extends CI_Loader into application/core and make load your interfaces.

Here has a sample: http://heatherevens.me.uk/2013/11/11/interfaces-in-codeigniter/


Depends on what you're asking. If you're asking if you can make the Code Igniter framework follow your Interfaces, you could, but it would require a whole lot of refactoring in their framework, and probably wouldn't be worth the work.

If you're asking if you can add Interfaces for your custom classes while using Code Igniter, of course you can. CI doesn't limit your ability to create custom code, in fact that's what it's there for. I guess the real answer is "What exactly are you asking?".


Your Interface is working great... Simply require_once it above the controller that will use it then declare a new instance of it from within the controller...

Controller File:

require_once( "Models/ServiceApi" );

class HomeController extends Controller
{
    private $repository;

    public function __Construct()
    {
        $this->repository = new ServiceApi();
    }

    ...
}

To be honest, you'd probably want to be able to do some kind of dependency injection, but considering I do not use codeigniter, I'm unsure of how easy it is to simulate a Factory for the dependant calls to Interface to classes.

Let me give you an example though:

Dependancy Injection:

Factory File

// Not sure how this works for code ignite but the idea is like this:

//$repositoryForController = new ServiceAPI();
$repositoryForController = new Service_Two_API();

$controller = new HomeController( $repositoryForController );

Controller File:

require_once( "Models/ServiceApi" );

class HomeController extends Controller
{
    private $repository;

    public function __Construct( BaseAPI $repo )
    {
        $this->repository = $repo;
    }

    ...
}
  • So Overview. Your interface and service APIS are fine
  • Although your architecture will come out tightly coupled, declaring a new instance of your model in a controller is not completely bad news.
  • To get the file into your controller file, just require_once it up above the controller.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜