[英]Codeigniter to call multiple functions within single statement
请看下面的示例代码。 从此我有了 index、test_1 和 test_2 函数。
如果执行索引函数的 case-1 语句,我将得到输出 12。但 case-2 语句得到错误消息:在 null 上调用成员函数 test_2()。
任何人都可以帮助我使 case-2 语句起作用吗?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Debug extends CI_Controller
{
public function index()
{
//case 1 : Working
$this->test_1();
$this->test_2();
//case 2: Not Working
echo $this->test_1()->test_2();
}
function test_1()
{
echo "1";
}
function test_2()
{
echo "2";
}
} ?>
提前致谢..
为了实现 $this->test1()->test2() 调用 $this 需要在每个函数中返回。
更新代码:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Debug extends CI_Controller
{
public function index()
{
//case 1 : Working
$this->test_1();
$this->test_2();
//case 2: Working
echo $this->test_1()->test_2();
}
function test_1()
{
echo "1";
return $this;
}
function test_2()
{
echo "2";
return $this;
}
} ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.