Bayu Angga Satrio
Bayu Angga Satrio

Reputation: 131

codeigniter: how to access more than one variable of other functions in one class?

For example I have a class like this

public function data() {
     $a = 1;
     $b = 2;
     $c = 3;
}

public function codeigniter() {
     $a, $b, $b //how i can get the value??
}

How can I get the value of $a, $b, $c so that I can use it in codeigniter() function? I use codeigniter? Do I need to add a return;?

Upvotes: 0

Views: 1064

Answers (3)

Bilal Tariq
Bilal Tariq

Reputation: 1

To get more than one variable from a function you must place those variables in an array and return the array

public function data() {

$a = 1;
$b = 2;
$c = 3;

$data = array();
array_push($data, $a,$b,$c);
return $data;
}//data function ends


public function codeigniter() {
    $data = $this->data();
//now use $data
}

Upvotes: 0

Sathya Baman
Sathya Baman

Reputation: 3515

First create a array and store the values in it and return the array.

public function data() {
    $data = array();
    $data['a']=1;
    $data['b']=2;
    $data['c']=3;
    return $data;
}

Then you can simply call the function and access the returned data.

    public function codeigniter() {
     $data_value = $this->data();

          echo $data_value['a'];
          echo $data_value['b'];
          echo $data_value['c'];
      }

Upvotes: 2

Oli Soproni B.
Oli Soproni B.

Reputation: 2800

I assume the two function are in both class and in a controller class of codeigniter

class Sample extends CI_Controller {
// declare variables
    public $a;
    public $b;
    public $c;

    public function __construct() {
        // call codeigniter method
        $this->codeigniter();
    }

    public function data() {
        $this->a = 10;
        $this->b = 11;
        $this->c = 12;
    }

    public function codeigniter() {

        // call method data();

        $this->data();

        echo $this->a; // prints 10
        echo $this->b; // prints 11
        echo $this->c; // prints 12


    }

}

Upvotes: 5

Related Questions