accesing variable of parent function

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
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

accesing variable of parent function

Post by devendra-m »

Code: Select all

<?
	function test1()
	{
		$count=0;
		function test2()
		{
			$count=1;
			print($count);
		}
		test2();
	}
	print(test1());
?>

in function test2(), is it possible to access variable $count of function test1(). Because I have to check value of variable of parent function while using recursion.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yes it is possible. But it would have been easy enough to try yourself ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There is no such thing as "parent" functions in PHP.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

A function can only access it's parameters and global variables.

... I had no idea PHP supported nested functions, though I hardly see it as a useful 'feature.'
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

superdezign wrote:A function can only access it's parameters and global variables.

... I had no idea PHP supported nested functions, though I hardly see it as a useful 'feature.'
It doesn't support nested functions. It supports the declaration of them anywhere for the most part however.
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Post by devendra-m »

In that case I think I should follow "Pass by reference" mechanism
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Why not just pass it, period?
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post by crystal ship »

Why not just pass it, period?
Are you talking about passing the value $count as parameter to the function test2() as function test2($count).
I think this will work well here
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Post by devendra-m »

But I need changed value of $count inside function test2(), So I need "pass by reference"
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Check out "passing variables by association" - it might be just what you're looking for.
User avatar
webspider
Forum Commoner
Posts: 52
Joined: Sat Oct 27, 2007 3:29 am

Post by webspider »

Kieran Huggins wrote:Check out "passing variables by association" - it might be just what you're looking for.
what does it mean by "passing variables by association"?as i know variables can be send to a function either passing by value or by reference. is it another way to pass variables to functions? how ?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

webspider wrote:what does it mean by "passing variables by association"?as i know variables can be send to a function either passing by value or by reference. is it another way to pass variables to functions? how ?
I've no idea what that is either. :?

You probably won't need to pass by reference though since you could always just return a value, and you definitely won't need to define that function within another, especially if recursion is what you're after.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

sorry - I meant by reference :oops:

I've been in ORM records all day :-)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

And make sure to read up on the difference of passing by reference on PHP 4 vs. PHP 5.
Post Reply