DillGates K
DillGates K

Reputation: 51

Codeigniter to call multiple functions within single statement

Please have a look at the below example code. From that I have index, test_1 & test_2 functions.

If the index function's case-1 statements executed, I'm getting output 12. But case-2 statement getting the error Message: Call to a member function test_2() on null.

Can anyone help me to make the case-2 statement work?

<?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";
  }
 } ?>

Thanks in advance..

Upvotes: 0

Views: 435

Answers (1)

DillGates K
DillGates K

Reputation: 51

In order to achieve the $this->test1()->test2() call $this need to be returned in each of the functions.

Updated Code:

<?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;
 }
} ?>

Upvotes: 1

Related Questions