The Truth About Lambdas in PHP

  • 1,643 views
Uploaded on

This is a static version of the dynamic, multi-media presentation I'm doing at CSUF on Saturday Jan 29, 2011. In other words, what's up at slideshare is the result of a cut and paste job of web …

This is a static version of the dynamic, multi-media presentation I'm doing at CSUF on Saturday Jan 29, 2011. In other words, what's up at slideshare is the result of a cut and paste job of web browser screen shots. Some of the code that is line numbered unfortunately appears out of kilter as a consequence of HTML and my word-processor being out of sinc with one another. Also, you may need to view the document in full screen mode and use the zoom-in button.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,643
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
0
Comments
0
Likes
4

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. flickr.com/photos: katkun/4349837202/By katkun
  • 2. 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 );
  • 3. 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");
  • 4. 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");
  • 5. 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");
  • 6. 1. <?php2. // practical use of closures: dynamic method creation3. // more info at http://www.ibm.com/developerworks/opensource/library/os-php- lambda/index.html?ca=drs-4. class Car { 5. public $model; 6. public function __construct( $type ) 7. { 8. $this->model = $type; 9. } 10.public function __call( $method, $args ) { 11.return call_user_func_array( $this->$method, $args ); 12.}13.}14.$car = new Car("Ferrari");15.$car->Drive = function( $speed ) use ( $car ) { 16.return Varoom! . $car->model . is driving . $speed . mph and loving it.;17.};18.$r = array();19.$r[0] = $car->Drive("90");20.$go_for_it = $car->Drive;21.unset($car);22.$r[1] = $go_for_it("120");23.include("ferrari.template.php");
  • 7. 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");