【问题标题】:codeigniter pass object library to another librarycodeigniter 将对象库传递给另一个库
【发布时间】:2016-01-24 13:23:54
【问题描述】:

我想在 Codeigniter 的库中翻译我的自定义类,但我遇到了这个问题。

require_once "a.php";
require_once "b.php";

function __construct() {    
     $this->b = new b();
}

我使用库加载器更改了require 和construct。

function __construct() {
   $this->CI =& get_instance();
   $this->CI->load->library('a');
   $this->CI->load->library('b');
}

但是现在我需要转换这个函数但是我不知道在哪种模式下可以做到这一点

public function addRecipient($first,$second){
   $a= new a($first);  // ??? new library a ?
   $a->header = 1;
   $a->set($second);     
   $this->b->add($a);     // passing object library a???
   return $a;
}

【问题讨论】:

    标签: php codeigniter object


    【解决方案1】:

    这是一个可能有帮助的逻辑

    制作一个管理员控制器 和 Amodel 和 Bmodel

    在管理控制器中加载这些模型

    class admin extends CI_Controller{
    
        function __construct()
        {
    
        $this-load>model('amodel');
        $this-load>model('amodel');
    
        }
    
     function index($first,$second){
     $a = $this->amodel->addRecipient($first,$second)
     $this->data['a'] = $a;
     $this->load->view('index',$this->data);
     }
    

    所以在 amodel.php 文件中你可以像这样调用 bmodel.php 函数

    public function addRecipient($first,$second){
    $a = new stdClass();
    $a->header = 1;
    $a->first = $first; 
    $a->second= $second;
    $a = $this->bmodel->get_todos($a);  
    return $a;
    }
    

    里面的bmodel.php函数可以是这样的

    function get_todos($a){
    
    $a->third =  $a->first + $a->second
    return  $a;
    
     }
    

    【讨论】:

      猜你喜欢
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多