Created
July 15, 2011 04:47
-
-
Save rgantt/1084089 to your computer and use it in GitHub Desktop.
Anonymous functions and closure in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$count = 0; | |
function increase_count() { | |
global $count; | |
$count++; | |
} | |
increase_count(); | |
echo $count; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var count = 2; | |
decrease_count = function() { | |
count--; | |
}; | |
decrease_count(); | |
document.write( count ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function create_closure( $count ) { | |
return function() use ( &$count ) { | |
return $count--; | |
}; | |
} | |
$closure = create_closure( 10 ); | |
$another_closure = create_closure( 20 ); | |
echo $closure(); // 10 | |
echo $another_closure(); // 20 | |
echo $closure(); // 9 | |
echo $closure(); // 8 | |
echo $another_closure(); // 19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function create_closure( $count ) { | |
return function() use ( &$count ) { | |
return $count--; | |
}; | |
} | |
$closure = create_closure( 10 ); | |
echo $closure(); // 10 | |
echo $closure(); // 9 | |
echo $closure(); // 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$count = 0; | |
function increase_count() { | |
global $count; | |
$count++; | |
} | |
increase_count(); | |
$increase_count_again = function() { | |
global $count; | |
$count++; | |
}; | |
$increase_count_again(); | |
echo $count; // should be 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$count = 0; | |
function increase_count() { | |
$count2 = 1; | |
function increase_count_again() { | |
global $count, $count2; | |
$count++; | |
$count2++; | |
} | |
increase_count_again(); | |
return $count2; | |
} | |
echo increase_count(); | |
echo $count; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$count = 1; | |
$decrease_count = function() use ( &$count ) { | |
$count--; | |
}; | |
$decrease_count(); | |
echo $count; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$count = 1; | |
$decrease_count = function() use ( $count ) { | |
$count--; | |
}; | |
$decrease_count(); | |
echo $count; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment