php-lambda-5.2

A small function for providing closure-like capabilities in PHP 5.2

  • Owner: lokothodida/php-lambda-5.2
  • Platform:
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

PHP Closures/Lambdas for 5.2

A small experiment to providing closure-like capabilities in PHP 5.2. This is
a limited project and is only intended as a small polyfill-like solution for
those with PHP hosts that still do not provide 5.3+ and would like some of the
benefits of 5.3+'s closures (namely variable importing).

Note: this solution currently does not pass the imported variables by
reference.

Initialization

Either load the global lambda function from this project:

// Defines global function 'lambda'
// Usage: lambda($params);
include 'lambda-global.php';

Or load the local version into a variable:

// Defines local function $lambda
// Usage: $lambda($params);
$lambda = include 'lambda-local.php';

Syntax

$lambda(array(
  'args' => array(/* */),  // Arguments for the new function
  'use'  => array(/* */),  // Variables to import into the scope of the function
  'fn'   => /* */,         // Body of the function
));

Examples

Basic usage

// Define some variables
$x = 3;
$y = 4;

// Create a closure that uses $x and $y
$myClosure = $lambda(array(
  'args' => array('$z'),
  'use'  => array('$x' => $x, '$y' => $y),
  'fn'   => 'return $z * $y * $z;'
));

$myClosure(5); // 60

External files

example.php

// Define some variables
$x = 3;
$y = 4;

// Create a closure that uses $x and $y
$myClosure = $lambda(array(
  'args' => array('$z'),
  'use'  => array('$x' => $x, '$y' => $y),
  'fn'   => 'myclosure.php'
));

$myClosure(5); // 60

myclosure.php

<?php
// Having a separate file allows you to define the function without building
// your own unweildly string in the 'fn' argument
return $z * $y * $z;

Shorthands

// If $params[0] exists but not $params['fn'], $params['fn'] will take
// $params[0]'s value:
$callback = $lambda(array('use' => array('$stuff' => 3), '
  var_dump($stuff);
'));

$callback(); // 3


Main metrics

Overview
Name With Ownerlokothodida/php-lambda-5.2
Primary LanguagePHP
Program languagePHP (Language Count: 1)
Platform
License:MIT License
所有者活动
Created At2015-08-07 19:50:02
Pushed At2015-08-07 21:22:30
Last Commit At2015-08-07 22:21:58
Release Count0
用户参与
Stargazers Count2
Watchers Count1
Fork Count0
Commits Count5
Has Issues Enabled
Issues Count0
Issue Open Count0
Pull Requests Count0
Pull Requests Open Count0
Pull Requests Close Count0
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private