如何将多个参数传递给CodeIgniter中的库? [英] How to pass multiple parameters to a library in CodeIgniter?

查看:91
本文介绍了如何将多个参数传递给CodeIgniter中的库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为CodeIgniter创建一个库,我想传递不同类型的多个参数(一个PDO对象,用户名和密码,配置等)。

I am making a library for CodeIgniter, and I wish to pass multiple parameters of different types (a PDO object, username and password, configurations, etc).

我知道我可以传递所有这些东西的数组,但这似乎不是最好的方式做事情( $ params 不能描述什么

I know I can pass an array of all of these things, but that doesn't seem to be the best way of doing things (as $params can't ever describe what is needed).

如何将多个参数传递给库?

How can I pass multiple parameters to a library?

p>

推荐答案

这个问题有几种方法。我会列出(以优先顺序)我知道解决它的方式:

There are several approaches to this particular problem. I'll list (in preferred order) ways I know to solve it:

关联数组参数

这种方法非常灵活,因为参数的顺序无关紧要,它解决了很多人对PHP定义函数参数的很大的抱怨。你只需传递你想要的非默认参数。

This approach is pretty flexible, as the order of the parameters doesn't matter, and it resolves a pretty big complaint many have with how PHP defines function parameters. You simply pass in the "non-default" parameters you want. This is probably the most "codeigniterish" way to do it, if that's even a thing.

class MyLibrary {

    public function __construct($params = array())
    {
        // Merge default parameter names and values,
        // with given $params array
        $params = array_merge(array(
            'server'   => 'myserver',
            'database' => 'mydatabase',
            'username' => 'myuser',
            'password' => 'mypassword'
        ), $params);

        // Create variables from parameter list
        extract($params);

        var_dump($server);
        var_dump($database);
        var_dump($username);
        var_dump($password);
    }

}

// Initialization:
$this->load->library('mylibrary', array(
    'server'   => 'server-arg1',
    'database' => 'database-arg2'
));

编号参数

此方法复制典型的PHP参数范例(定义所有预期参数的名称,顺序和默认值)。

This approach replicates the typical PHP parameter paradigm (defines names, orders, and default values for all expected parameters).

class MyLibrary {

    public function __construct($params = array())
    {
        // Add relevant defaults to missing parameters
        $params = array_merge($params, array_slice(array(
            'myserver',
            'mydatabase',
            'myuser',
            'mypassword'
        ), count($params)));

        // Create variables from parameter list
        extract(array_combine(array(
            'server',
            'database',
            'username',
            'password'
        ), $params));

        var_dump($server);
        var_dump($database);
        var_dump($username);
        var_dump($password);
    }

}

// Initialization:
$this->load->library('mylibrary', array('server-arg1', 'database-arg2'));

覆盖CI装载器类:

这是你自己的风险。基本上, CI_Loader :: _ ci_init_class()方法需要使用 MY_Loader 类和相应的方法重写。这些是你不喜欢的行(我的安装中的行1003-1012):

This is AT YOUR OWN RISK. Basically, the CI_Loader::_ci_init_class() method needs to be overridden with a MY_Loader class and corresponding method. These are the lines that you "don't like" (lines 1003-1012 in my install):

    // Instantiate the class
    $CI =& get_instance();
    if ($config !== NULL)
    {
        $CI->$classvar = new $name($config);
    }
    else
    {
        $CI->$classvar = new $name;
    }

我可以猜到的最安全的替换是:

The "safest" replacement that I could guess would be this:

    // Instantiate the class
    $CI =& get_instance();
    if (isset($config[1])
    {
        // With numeric keys, it makes sense to assume this is
        // is an ordered parameter list
        $rc = new ReflectionClass($name);
        $CI->$classvar = $rc->newInstanceArgs($config);
    }
    elseif ($config !== NULL)
    {
        // Otherwise, the default CI approach is probably good
        $CI->$classvar = new $name($config);
    }
    else
    {
        // With no parameters, it's moot
        $CI->$classvar = new $name;
    }

我真的不知道这会破坏多少事情,但我几乎肯定会说有什么,这不是真的值得的风险。我非常推荐上述第一种方法。

I really don't know how many things this will break, but I can almost certainly say there will be something. It's not really worth the risk. I'd STRONGLY recommend the first approach above.

干杯!

这篇关于如何将多个参数传递给CodeIgniter中的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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