flickr.com/photos: katkun/4349837202/By katkun
LOG.PHP:

       1. define("DEPOSIT",100);
       2. /**
       3. * getParams()
       4. * returns annual interest and how many hundreds
       5. **/
       6. function getParams() {
           7. $annual_interest = 0.05;
           8. $hundreds = 2;
           9. if ( isset( $_GET['ai'] ) ) {
            10.$annual_interest = (float) $_GET['ai'];
           11.}
           12.if (isset( $_GET['hu'] ) ) {
            13.$hundreds = (int) $_GET['hu'];
           14.}
           15.return array($annual_interest,$hundreds);
       16.}
       17.list ($annual_interest, $hundreds) = getParams();
       18.$percent_format = substr($annual_interest,-1,1) . '%';
       19.$rate = 1 + $annual_interest;
       20./** REMOVED:
       21.* $f = create_function('$x, $y', 'return round( log( $x ) / log( $y ),2 );' );
       22.*
       23.**/
       24.$f = function( $x, $y ) {
           25.return round( log( $x ) / log( $y ), 2 );
       26.};
       27.$x = $f( $hundreds, $rate );
THIS_AND_THAT.PHP:
      1. <?php
      2. class Example2 {
        3. private $secret_code = " <--{ -*- }--> ";
        4. function Square( $num ) {
         5. return $num * $num;
        6. }
        7. function getSecretSign()
        8. {
         9. return $this->secret_code;
        10.}
        11.function doIt( $x ) {
         12.$that = $this;
         13.return function ($y) use ($x, $that) {
             14.$special = $that->getSecretSign();
             15.return $special . ($that->Square( $x ) + $y) . $special;
         16.};
        17.}
      18.}
      19.$e2 = new Example2();
      20.$r = $e2->doIt( 10 );
      21.$rr = $r( 80 );
      22.include("this_and_that.template.php");
DIFFERENT_SCOPES.PHP:
      1. <?php
      2. include("lvd.header.php");
      3. ?>
      4. <script>
      5. var fact = function(n) {
        6. if( n <= 1 ){
         7. return 1;
        8. }
        9. else
        10.{
         11.return n * fact( n-1 );
        12.}
      13.};
      14.alert( fact( 4 ) );
      15.</script>
      16.<?php
      17.$fact = function( $n ){
        18.if( $n <= 1 ) {
         19.return 1;
         20.} else {
           21.return $n * $fact( $n-1 );
         22.}
        23.};
        24.$r = $fact( 4 ); // Output : Error
        25.include("lvd.footer.php");
DIFFERENT_SCOPES_GOOD.PHP:
      1. <?php include("lvd.header.php"); ?>
      2. <script>
      3. var fact = function( n ) {
        4. if( n <= 1 ){
         5. return 1;
         6. } else {
           7. return n * fact( n-1 );
         8. }
        9. };
        10.alert(fact(4))
        11.</script>
        12.<?php
        13.$fact = function( $n ) use( &$fact ) {
         14.if($n <= 1) {
           15.return 1;
         16.}
         17.else
         18.{
           19.return $n * $fact( $n-1 );
         20.}
        21.};
        22.$r = $fact( 4 );
        23.include("lvd.footer.php");
FERRARI.PHP:
       1. <?php
       2. // modified 1-24-2011
       3. // more info at
         http://www.ibm.com/developerworks/opensource/library/os-php-
         lambda/index.html?ca=drs-
       4. class Car {
        5. private $model;
        6. public $Drive;
        7. public function __construct( $model )
        8. {
          9. $this->model = $model;
        10.}
        11.public function __call( $method, $args ) {
          12.return call_user_func_array( $this->$method, $args );
        13.}
        14.public function __get($name) {
          15.return $this->$name;
        16.}
       17.}
       18.$car = new Car("Ferrari");
       19.$car->Drive = function( $speed ) use ( $car ) {
        20.return 'Varoom! ' . $car->model . ' is driving ' . $speed . ' mph and
              loving it.';
       21.};
       22.$r = array();
       23.$r[0] = $car->Drive("90");
       24.$go_for_it = $car->Drive;
       25.unset( $car );
       26.$r[1] = $go_for_it("120");
       27.include("ferrari.template.php");
INVOKE_EXAMPLE.PHP:

      1. <?php
      2. class Determinator {
       3. public $x;
       4. public function __construct( $x ) {
         5. $this->x = (int) $x;
       6. }
       7. public function __invoke() {
         8. $res = ($this->x % 2 == 1)? ' odd': 'even';
         9. return " // $d->x is $res <span style='font: 70% Arial,Helvetica'>
              ( " . $this->x . " )</span>";
       10.}
      11.}
      12.$num = pow(3,4) - 5;
      13.$d = new Determinator( $num );
      14.$r[] = $d();
      15.$d->x = pow(4,3) - 7;
      16.$r[] = $d->__invoke() . '<br>';
      17.include("invoke_example.template.php");
FERRARI2.PHP:
       1. <?php
       2. // modified 1-24-2011
       3. class Car {
        4. private $model;
        5. public function __construct( $model )
        6. {
          7. $this->model = $model;
        8. }
        9. public function __get($name) {
          10.return $this->$name;
        11.}
        12.public function Action( Closure $act, $speed=null ) {
          13.return $act($speed);
        14.}
       15.}
       16.$car = new Car("Ferrari");
       17.$closure = function( $speed ) use ( $car ) {
        18.return 'Varoom! ' . $car->model . ' is driving ' . $speed . ' mph and
              loving it.';
       19.};
       20.$lambda = function() {
        21.return "<strong>Hellow, World!</strong>";
       22.};
       23.$r = array();
       24.$r[0] = $car->Action( $lambda );
       25.$r[1] = $car->Action( $closure, "135");
       26.include("ferrari.template2.php");
The Truth About Lambdas in PHP
The Truth About Lambdas in PHP
The Truth About Lambdas in PHP
The Truth About Lambdas in PHP
The Truth About Lambdas in PHP

The Truth About Lambdas in PHP

  • 3.
  • 18.
    LOG.PHP: 1. define("DEPOSIT",100); 2. /** 3. * getParams() 4. * returns annual interest and how many hundreds 5. **/ 6. function getParams() { 7. $annual_interest = 0.05; 8. $hundreds = 2; 9. if ( isset( $_GET['ai'] ) ) { 10.$annual_interest = (float) $_GET['ai']; 11.} 12.if (isset( $_GET['hu'] ) ) { 13.$hundreds = (int) $_GET['hu']; 14.} 15.return array($annual_interest,$hundreds); 16.} 17.list ($annual_interest, $hundreds) = getParams(); 18.$percent_format = substr($annual_interest,-1,1) . '%'; 19.$rate = 1 + $annual_interest; 20./** REMOVED: 21.* $f = create_function('$x, $y', 'return round( log( $x ) / log( $y ),2 );' ); 22.* 23.**/ 24.$f = function( $x, $y ) { 25.return round( log( $x ) / log( $y ), 2 ); 26.}; 27.$x = $f( $hundreds, $rate );
  • 45.
    THIS_AND_THAT.PHP: 1. <?php 2. class Example2 { 3. private $secret_code = " <--{ -*- }--> "; 4. function Square( $num ) { 5. return $num * $num; 6. } 7. function getSecretSign() 8. { 9. return $this->secret_code; 10.} 11.function doIt( $x ) { 12.$that = $this; 13.return function ($y) use ($x, $that) { 14.$special = $that->getSecretSign(); 15.return $special . ($that->Square( $x ) + $y) . $special; 16.}; 17.} 18.} 19.$e2 = new Example2(); 20.$r = $e2->doIt( 10 ); 21.$rr = $r( 80 ); 22.include("this_and_that.template.php");
  • 49.
    DIFFERENT_SCOPES.PHP: 1. <?php 2. include("lvd.header.php"); 3. ?> 4. <script> 5. var fact = function(n) { 6. if( n <= 1 ){ 7. return 1; 8. } 9. else 10.{ 11.return n * fact( n-1 ); 12.} 13.}; 14.alert( fact( 4 ) ); 15.</script> 16.<?php 17.$fact = function( $n ){ 18.if( $n <= 1 ) { 19.return 1; 20.} else { 21.return $n * $fact( $n-1 ); 22.} 23.}; 24.$r = $fact( 4 ); // Output : Error 25.include("lvd.footer.php");
  • 51.
    DIFFERENT_SCOPES_GOOD.PHP: 1. <?php include("lvd.header.php"); ?> 2. <script> 3. var fact = function( n ) { 4. if( n <= 1 ){ 5. return 1; 6. } else { 7. return n * fact( n-1 ); 8. } 9. }; 10.alert(fact(4)) 11.</script> 12.<?php 13.$fact = function( $n ) use( &$fact ) { 14.if($n <= 1) { 15.return 1; 16.} 17.else 18.{ 19.return $n * $fact( $n-1 ); 20.} 21.}; 22.$r = $fact( 4 ); 23.include("lvd.footer.php");
  • 53.
    FERRARI.PHP: 1. <?php 2. // modified 1-24-2011 3. // more info at http://www.ibm.com/developerworks/opensource/library/os-php- lambda/index.html?ca=drs- 4. class Car { 5. private $model; 6. public $Drive; 7. public function __construct( $model ) 8. { 9. $this->model = $model; 10.} 11.public function __call( $method, $args ) { 12.return call_user_func_array( $this->$method, $args ); 13.} 14.public function __get($name) { 15.return $this->$name; 16.} 17.} 18.$car = new Car("Ferrari"); 19.$car->Drive = function( $speed ) use ( $car ) { 20.return 'Varoom! ' . $car->model . ' is driving ' . $speed . ' mph and loving it.'; 21.}; 22.$r = array(); 23.$r[0] = $car->Drive("90"); 24.$go_for_it = $car->Drive; 25.unset( $car ); 26.$r[1] = $go_for_it("120"); 27.include("ferrari.template.php");
  • 60.
    INVOKE_EXAMPLE.PHP: 1. <?php 2. class Determinator { 3. public $x; 4. public function __construct( $x ) { 5. $this->x = (int) $x; 6. } 7. public function __invoke() { 8. $res = ($this->x % 2 == 1)? ' odd': 'even'; 9. return " // $d->x is $res <span style='font: 70% Arial,Helvetica'> ( " . $this->x . " )</span>"; 10.} 11.} 12.$num = pow(3,4) - 5; 13.$d = new Determinator( $num ); 14.$r[] = $d(); 15.$d->x = pow(4,3) - 7; 16.$r[] = $d->__invoke() . '<br>'; 17.include("invoke_example.template.php");
  • 62.
    FERRARI2.PHP: 1. <?php 2. // modified 1-24-2011 3. class Car { 4. private $model; 5. public function __construct( $model ) 6. { 7. $this->model = $model; 8. } 9. public function __get($name) { 10.return $this->$name; 11.} 12.public function Action( Closure $act, $speed=null ) { 13.return $act($speed); 14.} 15.} 16.$car = new Car("Ferrari"); 17.$closure = function( $speed ) use ( $car ) { 18.return 'Varoom! ' . $car->model . ' is driving ' . $speed . ' mph and loving it.'; 19.}; 20.$lambda = function() { 21.return "<strong>Hellow, World!</strong>"; 22.}; 23.$r = array(); 24.$r[0] = $car->Action( $lambda ); 25.$r[1] = $car->Action( $closure, "135"); 26.include("ferrari.template2.php");