Parse error: syntax error, unexpected T_OBJECT_OPERATOR in line 89

I am just in the process of learning OOP PHP 5.

The error is in the foreach. I am probably doing something wrong with the start. It works when I write is procedurally. I'm sure this is a
syntax issue

Related issue is when should I use $this->variable and just $variable in a method.

public function sendTransaction()
{
//below is line 89
	foreach(this->$authnet_values as $key => $value ) 
	 $this->fields .= "$key=" . urlencode( $value ) . "&";
	 $this->ch = curl_init($this->auth_net_url); 
	 curl_setopt($this->ch, CURLOPT_HEADER, 0); /
	 curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($this->ch, CURLOPT_POSTFIELDS, rtrim( $this->fields, "& " )); 
	### curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
	$this->resp = curl_exec($this->ch); //execute post and get results
	curl_close ($this->ch);	

}


    Remove the $ sign.
    this->$authn ...
    to
    $this->authn

      The syntax error is because the "$" needs to be in front of the "this" instead of the class variable name.

      "$this->" is used when you want to reference an object variable or method. Within the above function, I can see no reason to use it with the cURL object "$ch", since that variable only exists within the lifespan of any individual call to that method.

        I've rewritten the code based on the advice not to use $this for variables only used in this function and corrected the really obvious syntax error I missed

        public function sendTransaction()
        {
        foreach(this->authnet_values as $key => $value ) 
        $fields .= "$key=" . urlencode( $value ) . "&";
        $ch = curl_init($this->auth_net_url); 
        curl_setopt($ch, CURLOPT_HEADER, 0); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim( $fields, "& " )); 
        ### curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
        $this->resp = curl_exec($ch); //execute post and get results
        curl_close ($ch);	
        }
        

        authnet_values is defined in the constructor method

         $this->authnet_values	= array
        		(
        		"x_card_num"		=> $this->x_card_num,
        		"x_exp_date"		=> $this->x_exp_date,
        		"x_description"	=> $this->description,
        		"x_amount"		=> $this->x_amount,
        		"x_first_name"		=> $this->x_first_name,
        		"x_last_name"		=> $this->x_last_name,
        		"x_address"		=> $this->x_address,
        		"x_address2"		=> $this->x_address2,
        		"x_city"			=> $this->x_city,
        		"x_state"			=> $this->x_state,
        		"x_zip"			=> $this->x_zip,
        		"x_login"			=> $this->x_login, 
        		"x_version"		=> $this->x_version,
        		"x_delim_char"		=> $this->x_delim_char,
        		"x_delim_data"	=> $this->x_data,
        		"x_type"			=> $this->x_type,
        		"x_method"		=> $this->x_method,
         		"x_tran_key"		=> $this->x_tran_key,
         		"x_relay_response"	=> $this->x_relay_response,
        
        	);
        
          foreach([color=red]$[/color]this->authnet_values as $key => $value )
          

            Yep was just about to post I was an idiot on that.

            Wish PHP gave clearer messages.

            Thanks

              Write a Reply...