首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Codeigniter在一条语句中调用多个函数

Codeigniter在一条语句中调用多个函数
EN

Stack Overflow用户
提问于 2020-01-04 23:59:52
回答 1查看 31关注 0票数 0

请看下面的示例代码。由此,我得到了索引、test_1和test_2函数。

如果执行索引函数的case-1语句,我会得到输出12。但是case-2语句会得到错误消息: Call to a member function test_2() on null。

有没有人能帮我使case-2语句起作用?

代码语言:javascript
复制
<?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";
  }
 } ?>

提前谢谢..

EN

回答 1

Stack Overflow用户

发布于 2020-01-05 02:22:31

为了实现$this->test1()->test2()调用,需要在每个函数中返回$this。

更新代码:

代码语言:javascript
复制
<?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;
 }
} ?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59592388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档