1

Let's say I have the following:

class Submit extends \RightNow\Models\Base
{
    function __construct()
    {
        parent::__construct();
    }
    function inner () {
        return $i++;
    }
    function outer(){
        $i = 0;
        $this->inner();
        echo $i;
    }
}

$submit = new Submit();
$submit->outer();

How is it possible for inner() to access the variable $i that was declared in outer()? I've tried using global but this didn't work.

I understand in my very basic example, I could pass the variable in as a parameter but this just serves as an example for my question

I am using CodeIgniter

1

3 Answers 3

4

As you mentioned you are using Codeigniter, You can achieve it by the below code. In any class, you can use a class member variable in any of the member functions in the class.

class Submit extends \RightNow\Models\Base
 {
  public $i = 0;
  function __construct()
   {
      parent::__construct();
   }
  function inner () {
    return $this->i++;
   }
  function outer(){
    $this->i = 0;
    inner();
    echo $this->i;
   }
  outer();
}
Sign up to request clarification or add additional context in comments.

Comments

1

If all your methods are in a class you can use the below approach whereby the variable is a class reference set in the class and so automatically available to all class methods.

Variables in the class are called properties.

class Submit extends \RightNow\Models\Base
{
    private $i = 0; // set class based variable;

    function __construct()
    {
        parent::__construct();
    }
    function inner () {
        return $this->i++;
    }
    function outer(){
       // $i = 0; You probably don't want to set this each call? 
        $this->inner();
        echo $this->i; 
    }
}

$submit = new Submit();
$submit->outer();

See above everything references the class property $i which is set in the class.

Alternatively if you're working with the same process over multiple varaibles from you can group them up into an array and simply pass the array as the single argument.

Your code will look slightly different depending on if the values are coming from inside or outside the class.

References :

PHP - passing variables to classes

https://www.php.net/manual/en/language.oop5.basic.php

https://www.php.net/manual/en/language.oop5.properties.php

Comments

0

You can pass a variable by reference. E.g.:

function inner (&$i) {
    return $i++;
}

function outer(){
    $i = 0;
    inner($i);
    echo $i; // 1
}

UPD

If you want to pass multiple variables:

  1. you can pass an array by reference:
function inner (&$arr) {
   $arr['i']++;
   $arr['j']--;
}

function outer() {
   $a = array('i' => 5, 'j' => 10);
   inner($a);
   echo $a['i'] . "\n" . $a['j']; // 6 9
}
  1. You can pass an object. In this case it always passed by reference implicitly. E.g.
function inner($obj) { // no & needed, because it is an object reference itself
    $obj->a++;
    $obj->b--;
}


function outer() {
    $obj = new \stdClass();
    $obj->a = 5;
    $obj->b = 10;
    inner($obj);
    echo $obj->a . "\n" . $obj->b;  // 6 9
}
  1. inside the same class you can use private members, but make sure the functions are not recursive (i.e. the same field is not reused simultaneously in different places):
class Submit extends \RightNow\Models\Base
{
    private $_i;

    function __construct()
    {
        parent::__construct();
    }
    function inner () {
        $this->_i++;
    }
    function outer(){
        $this->_i = 0;
        $this->inner();
        echo $this->_i;
    }
}

1 Comment

So if I had a dozen parameters, I'd have to do this a dozen times? That's the only way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.