user656925
user656925

Reputation:

Using the same variable name for a function's argument and its return value*

In

public function bind($query, $input_param, $btypes)
{
    // $input_param = $this->ref_arr($input_param);  // this self assignment gives an error!
    $input_ref = $this->ref_arr($input_param);       // this works 
}

I learned this by trial and error... but I'm trying to figure out why?

I haven't had a chance to form more test cases but if I use $input_param as in input to the function I can not return the result back to $input_param. Once I change the name to something else, in this case $input_ref it works.

Upvotes: 0

Views: 119

Answers (2)

user656925
user656925

Reputation:

I don't have time to reproduce this...simply changing the variable name fixed the problem.

Upvotes: 0

dqhendricks
dqhendricks

Reputation: 19251

The $this keyword references the current object you are in.

So if you are in code that is in a class like this:

class foo {
   public function __construct() {
      $this->bar = 'that'; // works because $this references the foo object
   }
}

Should work. In the case where you are outside of an object however, $this would not work, because there is no object for $this to reference.

class foo {
   public function __construct() {

   }
}
$this->bar = 'that'; // will not work because you are not inside of any object

Upvotes: 2

Related Questions