exit a method and store a variable

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
stevestark5000
Forum Commoner
Posts: 61
Joined: Thu Jan 27, 2011 12:08 am

exit a method and store a variable

Post 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);

?>
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: exit a method and store a variable

Post 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'


?>
Post Reply