Pass-by-reference help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Pass-by-reference help

Post by pickle »

Hi everyone,

I'm a little (ok, a lot) new to the whole concept of passing-by-reference, so I was wondering if I could get a little help.

I've got an object that I'm using to extract billing data from a database. Depending on the data, I'm extracting from two different tables, with the data going into different local object variables.

In the constructor function, I'm creating an array like this:

Code: Select all

$my_array = array('db_field_1'=>&$this->local_object_var_field_goes_into);
...
Which is working I think (I'm not sure how to check that anyway).

The problem I'm having is in another function, when I'm trying to pull those variables out of the array. I've got a line like this:

Code: Select all

foreach($retrieval_params['vars_to_store'] as $db_field=>&$object_var)
But I'm getting a parse error:
PHP wrote: Parse error: parse error, expecting `T_VARIABLE' or `'$'' in /path/to/file/beta_reporting/data.php on line 188
Line 188 is the above mentioned line (and is line 63 in the code below).

The reason I'm trying to do it like this, is because I'd like to be able to change the object variables, but store them in an array so my storing function doesn't need to explicitly know which object variable it's setting.


Full code:

Code: Select all

function retrieve_current_data($use_db)
  {
    echo "<u>$use_db</u><br />";

    $ret_val = true;

    //---------------------------------------------------
    //import retrieval parameters from object to function
    $retrieval_params = $this->current_retrieval_function_params[$use_db];

    //------------------------------------------------------
    //determine what to search on.  Use username if it's set
    //otherwise use passed credentials
    if(strlen($this->username) > 0)
    {
      $using_identifier = $this->username;
      $using_identifier_type = $retrieval_params['default_identifier_type'];
    }
    else
    {
      $using_identifier = $this->identifier;
      $using_identifier_type = $this->identifier_type;
    }


    //--------------------------------
    //determine which DB object to use
    $db_obj_to_use;
    if($use_db == 'table1')
    {
      $db_obj_to_use =& $this->DB1;
    }
    else if($use_db == 'table2')
    {
      $db_obj_to_use =& $this->DB2;
    }

    //----------------
    //get data from DB
    $query = <<<SQL
SELECT
      *
FROM
      $retrieval_params[table]
WHERE
      $using_identifier_type = '$using_identifier'
$retrieval_params[order_clause]
SQL;

    $result = $db_obj_to_use->sql_query($query,"retrieving $use_db data");


    //--------------------
    //store data in object
    if($this->debug && $db_obj_to_use->count_rows($result) == 0)
    {
      $this->set_debug("No $use_db data found for $using_identifier_type = $using_identifier");
    }
    else if($db_obj_to_use->count_rows($result))
    {
      $row = $db_obj_to_use->get_data($result);
      foreach($retrieval_params['vars_to_store'] as $db_field=>&$object_var) //<-- line that's causing problems
      {
	   $object_var = $row[$db_field];
	
	   if(!strlen($object_var))
	   {
	     $ret_val = false;
	   }
      }
    }
    return($ret_val);
  }
If anyone has any questions at all, please don't hesitate to ask. Like I said, I'm new to passing by reference, so my explanation of the concept might not be that effective.

Thanks in advance!


[ EDIT ]
Alternatively, is there a way to dynamicially call object variables? I know you can do:

Code: Select all

${"th_".$image_name}
but can you do something like that with object variables:

Code: Select all

${"this->".$object_variable_name}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply