php 通过将参数传递给 codeigniter 中的构造函数来加载库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10655107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:43:56  来源:igfitidea点击:

Load library by passing parameter to constructor in codeigniter

phpcodeigniter

提问by piyush

I am using codeigniter. I defined a library in code-igniter and is expecting a parameter in its constructor.This is my library code -

我正在使用代码点火器。我在 code-igniter 中定义了一个库,并且在其构造函数中需要一个参数。这是我的库代码 -

################# [My Library Code Test_lib.php ] ########################
<?php
class Test_lib
{
var $params;
public function __construct($params)
{
    $this->params = $params;
    echo $this->params;
}
}

In codeigniter documentation, it is mentioned that you can pass the parameter in the second argument. So,I am initializing it from controller as below -

在 codeigniter 文档中,提到您可以在第二个参数中传递参数。所以,我从控制器初始化它如下 -

<?php
class Test_cont extends CI_Controller {
function __construct()
{
    parent::__construct();
}

function test()
{
    $params = "abc";
    $this->load->library("test_lib",$params);
}
}   

I am getting following error -

我收到以下错误 -

A PHP Error was encountered Severity: Warning Message: Missing argument.....

遇到 PHP 错误严重性:警告消息:缺少参数.....

Please assist.

请协助。

回答by Colin Brock

$paramsmust be an array. From the documentation:

$params必须是数组。从文档

In the library loading function you can dynamically pass data as an array via the second parameter and it will be passed to your class constructor:

$params = array('type' => 'large', 'color' => 'red');

$this->load->library('Someclass', $params);

在库加载函数中,您可以通过第二个参数将数据作为数组动态传递,并将其传递给您的类构造函数:

$params = array('type' => 'large', 'color' => 'red');

$this->load->library('Someclass', $params);

In your case, you want to do something like this:

在你的情况下,你想做这样的事情:

function test()
{
    $params[] = "abc"; // $params is an array
    $this->load->library("test_lib",$params);
}

回答by Moyed Ansari

You just need to modify the $paramsfrom a variable to array. hope this will work

您只需要将$params从变量修改为数组。希望这会奏效

function test()
{
    $params = array(1=>'abc');
    $this->load->library('test_lib',$params);
}

回答by antelove

Class Test_lib

类 Test_lib

################# [My Library Code Test_lib.php ] ########################
<?php

class Test_lib {

  var $parameter1;
  var $parameter2;
  var $parameter3;

  public function __construct($parameters) {

    /* array assoc */
    $this->parameter1 = $parameters['argument1'];
    $this->parameter2 = $parameters['argument2'];
    $this->parameter3 = $parameters['argument3'];

    /* object */
    $oParameters = (object) $parameters;

    $this->parameter1 = $oParameters->argument1;
    $this->parameter2 = $oParameters->argument2;
    $this->parameter3 = $oParameters->argument3;

  }

}

Class Controller

类控制器

<?php

  class Test_cont extends CI_Controller {

    function __construct() {
      parent::__construct();
    }

    function test() {

      $data = array( // array assoc
        'argument1' => 'String 1',
        'argument2' => 'String 2',
        'argument3' => 'String 3'
      );

      $this->load->library("Test_lib", $data);

      return $this->Test_lib->parameter1;

    }

    echo $this->test(); // String 1

  }