图书馆作为Singleton在Codeigniter? [英] Library as Singleton in Codeigniter?

查看:111
本文介绍了图书馆作为Singleton在Codeigniter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用单例模式创建了一个自定义库。为什么要这样呢?因为我需要能够调用我的函数而不用 $ this - > 引用,例如,我可以执行下面的代码:

  function foo(){
MyLibrary :: instance() - > foo()
}

那么我可以像这样在控制器中调用我的函数:

  function foo(); 



的实例

  $ this-> mylibrary-> foo(); 

我有麻烦,因为CodeIgniter尝试instanciate我的库,当最好的方法是读静态实例。



为什么我需要这样做?



我的自定义库必须从外部php文件注册hooks in / plugins目录外部的应用程序文件夹。看:




  • / application

  • / plugins

  • /plugins/helloworld.php



这是我的Loader程式库:

  class Loader {

public static $ instance;
protected $ hooks;

私有函数__construct($ params = array()){
$ this-> hooks = array();
$ this-> includePlugins();
}

public static function instance()
{
if(!self :: $ instance)
{
self :: $ instance = new Loader();
}

return self :: $ instance;
}

public function includePlugins()
{
include_once/plugins/helloworld.php;
}

public function do_action($ hook =after)
{
foreach($ this-> actions [$ hook] as $ function)
{
call_user_func_array($ function,array());
}

}

public function add_action($ hook,$ function,$ priority)
{
if($ hooks! = false)
{
$ this-> actions [$ hook] [] = $ function;
}
else
{
echo钩名称不可用;
}
}

}



/ **
*使用Singleton实例的函数! !
* /

function do_action($ hook)
{
Loader :: instance() - > do_action
}

function add_action($ hook,$ function,$ priority)
{
Loader :: instance() - > add_action ,$ priority);
}

否,my helloworld.php(plugin)看起来像这样:

  add_action('right_here','show_message',11) 
function show_message()
{
echohello world!;
}



最后,我可以在我的控制器/视图中调用我的函数do_action以打印此消息,如下所示:

  do_action('right_here');注意,我所有的重要函数都是通过一个全局函数来调用的,它允许我使用单例实例。但我遇到两个问题:


  1. Codeigniter需要我的结果公开为了
    instanciate。

  2. 如果我将其公开,则会创建多个实例,并且库
    将无法按预期工作。


解决方案

我已解决问题:)



要创建单例库,您必须:




  • 在system / core / Codeigniter.php中包含您的库如下:
    require_once(/ path / to / library.php);


  • 使用& ,如下所示:




单身:

  public static function& instance()
{
if(!self :: $ instance)
{
self :: $ instance = new Loader ();
}

return self :: $ instance;
}




  • 准备使用您的功能。此外,您可以在库中加入CI
    对象。


I've created a custom library using singleton pattern. Why by this way? because I need to be able to call my functions without $this-> reference, for example, I could execute the code below:

function foo() {
    MyLibrary::instance()->foo();        
}

then I could call my functions in controllers like this:

function foo();

instance of

$this->mylibrary->foo();

I am in trouble because CodeIgniter try to instanciate my library when the best way to do is "read" the static instance.

Why I need to do this?

My custom library must register "hooks" from external php files located in /plugins directory outsite of application folder. Look:

  • /application
  • /plugins
  • /plugins/helloworld.php

This is my Loader library:

class Loader{

    public static $instance;
      protected $hooks;

    private function __construct($params = array()){
        $this->hooks= array();
        $this->includePlugins();
    }

    public static function instance()
        {
            if (!self::$instance)
            {
                self::$instance = new Loader();
            }

            return self::$instance;
        }

    public function includePlugins()
    {
        include_once "/plugins/helloworld.php";  
    }

    public function do_action($hook= "after")
    {
        foreach ($this->actions[$hook] as $function)
        {
            call_user_func_array($function, array());
        }

    }

    public function add_action($hook, $function, $priority)
    {
        if ($hooks !== false)
        {
        $this->actions[$hook][] = $function;
        }
        else
        {
        echo "hook name is unavaliable";
        }
    }

}



/**
 * Functions using the Singleton instance !!!
*/

function do_action($hook)
{
    Loader::instance()->do_action($hook);
}

function add_action($hook, $function, $priority)
{
    Loader::instance()->add_action($hook, $function, $priority);
}

No, my helloworld.php (plugin) looks like this:

add_action('right_here', 'show_message', 11);
function show_message()
{
    echo "hello world!";
}

Finally, I could call my the function do_action in my controller/view in order to print the message, like this:

do_action('right_here');

Note that all my important functions are called by a global function that allow me to use the singleton instance. But I am experiencing two issues:

  1. Codeigniter needs that my contruct be public in order to instanciate. Here I need to take my singleton.
  2. If I let it public, several instances will be created and the library will no longer works as expected.

解决方案

I've solved the problem :)

In order to create a singleton library you must:

  • Include your library at system/core/Codeigniter.php like this: require_once("/path/to/library.php");

  • Make your instance return with & symbol, like this:

Singleton:

public static function &instance()
{
    if (!self::$instance)
        {
            self::$instance = new Loader();
        }

        return self::$instance;
}

  • Ready to uses your functions as well. Also, you can include th CI object in you library.

这篇关于图书馆作为Singleton在Codeigniter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆