Page 1 of 1

exit a method and store a variable

Posted: Wed Mar 16, 2011 8:06 pm
by stevestark5000
If this can't be done then can someone say so please. in the example below, is there a way to get the variable of $firstn when the method setData exits? for example, I called method setData with the $firstn parameter. now that the method setData has exited, I would like to store the $firstn variable with the variable that setData exited with. Is this possible?

Code: Select all

<?
class Customer {
 var $firstname;
 
	function setData($firstname1) {
		$firstname = $this->firstname1;
		echo $this->firstname;
	}
}

$test = new Customer;
$test -> firstname = "test";
$test -> setData($firstn);

?>

Re: exit a method and store a variable

Posted: Thu Mar 17, 2011 4:58 am
by Kadanis
You need to return the variable

Code: Select all


<?
class Customer {
 var $firstname;
 
        function setData($firstname1) {
                $firstname = $this->firstname1;
                echo $this->firstname;
                return $this->firstname;
        }
}

$test = new Customer;

$firstn = 'Bob';

$new_first_name = $test -> setData($firstn);  //$new_first_name will equal 'Bob'


?>