【问题标题】:Passing value to another class php将值传递给另一个类 php
【发布时间】:2019-07-10 13:59:14
【问题描述】:

好吧,我有一个名为 preFinal() 的函数,它计算数据库中的值,我想将 preFinal() 的 $this->Result 传递给另一个控制器。 Cz 我需要在 B_Class() 控制器中计算 $this->Result

<php?

class A_Class extends CI_Controller {

public $Result;
public function __construct() {

    parent :: __construct();
    $this->load->model('m_hitung');
    $this->models = $this->m_hitung->datasee();
}

public function index () {
    $this->get_data();
    $this->DSR();

}

public function get_data () {

    foreach($this->models as $ambil) {
            $this->get_DSR[] = $ambil->dsrr;
        }

}

public function preFinal() {
    for($i =0; $i < count($this->get_DSR); $i++){
        if ($this->get_DSR[$i] >= 0 && $this->get_DSR[$i] < 40) {
        $this->Result[$i] = 1 * 50;
        }
    }

    return $this->Result;
}

}
?>

//and this is how I made B_class, and I don't know what to code

class B_class extends A_Class {

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

}

public function index () {
    print_r($this->preFinal());
}


}

当我尝试调用该函数时,它有关于加载会话失败的消息

【问题讨论】:

  • 据我回忆,CI 不支持在控制器之间直接传递数据。您可以做的是使用 flashdata 存储数据,然后加载第二个控制器,然后读取存储在 flashdata 中的数据。它快速、简单且相对无痛
  • 确切的错误是什么?

标签: php database codeigniter


【解决方案1】:

然后你可以在你的构造函数中插入你的类并使用你想要的函数:

$protected $classA;

public function __construct(A_Class  $classA)
{
    parent :: __construct();
    $this->classA = new $classA();
}

在你的函数里面你可以做:

 print_r($this->classA->preFinal());

另一件事是您可以将控制器加载为库,例如:

$this->load->library('../controllers/classA');

再次像我们之前提到的那样在你的函数中访问它

【讨论】:

  • 仍然出现错误,比如“无法定位指定的类:Session.php”
  • 在您的 b 类中,您也应该扩展 CI_Controller。
猜你喜欢
  • 2014-02-24
  • 1970-01-01
  • 2018-02-05
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
  • 2013-12-10
  • 2013-07-24
相关资源
最近更新 更多