[英]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.