https://drupal.org/node/1578558 에서 참조

 

Contextual filters

Last updated May 14, 2012. Created by Itangalo on May 14, 2012.
Log in to edit this page.

Views is a highly flexible module to start with, but the contextual filters increase the use cases for the module by an order of magnitude. Contextual filters work similarly to regular filters, but there is one important difference. Instead of setting a filter value manually, the value is fetched from variables sent programmatically to the view. A regular filter could give you all nodes written by a specified user. A contextual filter for a node author would be able to display all nodes written by the currently viewed user, or the same user who wrote the currently viewed node. The concept is that contextual filters prepare a view for filtering, but the filter value is not yet determined. When the view is eventually called, it is also provided with data used to complete the contextual filters.

The classic example of how contextual filter values are provided to views is by the view path. If a view has the path example.com/my-view, the URL example.com/my-view/story/22 will call the view along with two values for contextual filters (in this case story and 22). But there are more ways of providing contextual filter values. These are discussed in Chapter about Page manager and Panels.

The difference between standard filters and contextual filters may seem trivial, but in practice it is huge. Contextual filters allow you to reuse views in many different situations,and allows Views to interact with other parts of the website. It is not uncalled-for to write a whole book about how to use contextual filters in Views.

TIP: Until recently, contextual filters were called arguments in Views and a lot of documentation, tutorials and Views-compatible modules still use that term. If you see the term argument, it should be interpreted either as a contextual filter, or the value provided to a contextual filter.

 

http://blog.dubbelboer.com/2012/04/07/php-use-keyword.html 에서 참조

 

Anonymous functions

As of 5.3.0 PHP has support for anonymous functions. There are two well known and one lesser known way to pass variables to your anonymous functions.

  1. Using the global scope.
  2. Passing them as arguments (optionally as reference if you want to be able to modify them).
  3. Using the use keyword.

The use keyword

The use keyword allows you to introduce local variables into the local scope of an anonymous function. This is useful in the case where you pass the anonymous function to some other function which you have no control over.

In this simple example we have no control over which arguments array_filter passes to our anonymous function. The only other way to access the $half variable in this example function would be to make it a global variable. The use keyword allows us to solve this elegantly without polluting our global scope.

<? function remove_lowest_half($arr) { $half = array_sum($arr) / count($arr); // Calculate the average value. return array_filter($arr, function($v) use ($half) { return ($v > $half); }); } // 8 elements which sum up to 40. // So half will be 5. $input = array(1,2,3,4,6,7,8,9); var_dump(remove_lowest_half($input)); 

This will output the expected:

array(4) {

  [4]=>

  int(6)

  [5]=>

  int(7)

  [6]=>

  int(8)

  [7]=>

  int(9)

}

Variables passed through use are not passed by reference. Like normal function arguments modifying them will not modify the original. The usage of & to make them references is possible.

Using this we can build another example. A simple counting function with an initial value using no global variables for the counting.

<? function counter($start) { $value = $start; return function() use (&$value) { return $value++; }; } $counter = counter(6); echo $counter() . "n"; echo $counter() . "n"; 

This will output the expected:

6

7

Note: use can only be used with anonymous functions. Using it with named functions gives a syntax error.

http://www.recessframework.org/page/functional-php-anonymous-functions-lambdas-closures 에서 참조

One of the most exciting features of PHP 5.3 is the first-class support for anonymous functions. You may have heard them referred to as closures or lambdas as well. There’s a lot of meaning behind these terms so let’s straighten it all out.

What is the difference between Anonymous Functions, Lambdas, and Closures?

You’ll see the terms “anonymous functions”, “lambdas”, and “closures” thrown around in reference to the new features of PHP 5.3. Even the the URL php.net/closures redirects to php.net/manual/functions.anonymous.php. The difference between ‘lambda’ and ‘anonymous function’? None, for all intents and purposes they are two words for the same concept which is a descendant of lambda calculus. Languages with anonymous functions consider functions to be first-class value types, just like integers or booleans. Anonymous functions can thus be passed as arguments to another function or even returned by a function. Let’s make this concrete in PHP:

  1. <?php   
  2. $lambda = function() {    
  3.             echo ”I am an anonymous function,    
  4.                   aka a lambda!<br />”;   
  5.             };   
  6. $anonymousFunction = $lambda;   
  7. $anonymousFunction();    
  8. // Output: I am an anonymous function, aka a lambda!   
  9.   
  10. function nCallsTo($n$function) {   
  11.     for($i = 0; $i < $n$i++) {   
  12.         $function();   
  13.     }   
  14.     return function() { echo “I am also an anonymous function!<br />”; };   
  15. }   
  16.   
  17. $anotherAnon = nCallsTo(3, $anonymousFunction);   
  18. // Output:   
  19. // I am an anonymous function, aka a lambda!   
  20. // I am an anonymous function, aka a lambda!   
  21. // I am an anonymous function, aka a lambda!   
  22.   
  23. $anotherAnon();   
  24. // Output: I am also an anonymous function!   
  25. ?>  
<?php

$lambda = function() { 

            echo "I am an anonymous function, 

                  aka a lambda!<br />";

            };

$anonymousFunction = $lambda;

$anonymousFunction(); 

// Output: I am an anonymous function, aka a lambda!



function nCallsTo($n, $function) {

    for($i = 0; $i < $n; $i++) {

        $function();

    }

    return function() { echo "I am also an anonymous function!<br />"; };

}



$anotherAnon = nCallsTo(3, $anonymousFunction);

// Output:

// I am an anonymous function, aka a lambda!

// I am an anonymous function, aka a lambda!

// I am an anonymous function, aka a lambda!



$anotherAnon();

// Output: I am also an anonymous function!

?>

Notice how we did not assign a name to the function, we assigned a function to be the value of a variable. Just like a string or any other primative. We then assign it to another variable. The function is just a value, it has no name, hence the term “anonymous function”. We then create a regular function named nCallsTo that takes two arguments, $n being the number of times to make a call to $function an anonymous function.

The existance of higher-order functions opens the door for techniques like map/reducenext post.nCallsTo is a higher-order function on two accounts: 1) it takes a function as an argument, and 2) it returns a function as a value. Higher-order functions open the doors for techniques like map/reduce and deserves a post in itself. The point is lambdas and anonymous functions are the same things: functions that are values.

If anonymous functions are values, what does PHP consider their type to be? Let’s find out:

  1. <?php    
  2. $lambda = function() { echo “anonymous function”; };   
  3. echo gettype($lambda) . ‘<br />’;   
  4. // Output: object   
  5. echo get_class($lambda) . ‘<br />’;   
  6. // Output: Closure   
  7. ?>  
<?php 

$lambda = function() { echo "anonymous function"; };

echo gettype($lambda) . '<br />';

// Output: object

echo get_class($lambda) . '<br />';

// Output: Closure

?>



On the Closure object in PHP 5.3

What is a closure? So far it’s a misnomer. We haven’t actually spotted a closure even though PHP assigns all anonymous functions the type Closure. Since we haven’t actually seen a closure yet, let’s take a look at one:

  1. <?php   
  2. function letMeSeeAClosure() {   
  3.     $aLocalNum = 10;   
  4.     return function() use (&$aLocalNum) { return ++$aLocalNum; };   
  5. }   
  6. $aClosure = letMeSeeAClosure();   
  7. echo $aClosure();   
  8. // Output: 11   
  9. echo $aClosure();   
  10. // Output: 12   
  11. $anotherClosure = letMeSeeAClosure();   
  12. echo $anotherClosure();   
  13. // Output: 11   
  14. echo $aClosure();   
  15. // Output: 13   
  16. echo $aLocalNum;   
  17. // Notice: Undefined Variable: aLocalNum<BR>?>  
<?php

function letMeSeeAClosure() {

    $aLocalNum = 10;

    return function() use (&$aLocalNum) { return ++$aLocalNum; };

}

$aClosure = letMeSeeAClosure();

echo $aClosure();

// Output: 11

echo $aClosure();

// Output: 12

$anotherClosure = letMeSeeAClosure();

echo $anotherClosure();

// Output: 11

echo $aClosure();

// Output: 13

echo $aLocalNum;

// Notice: Undefined Variable: aLocalNum
?>

Chew on that for a minute. Do you spot the funny business? $aLocalNum is a local variable defined within the scope of the plain-old function letMeSeeAClosure. With the new use syntax the variable $aLocalNum is bound or closed over to create the closure. This allows the returned Closure to retain a reference to $aLocalNum, even after $aLocalNum falls out of lexical scope when the function returns. The notice error occurs when trying to reference $aLocalNum directly from outside of the function’s scope.

To recap, the terms, ‘lambda’ or ‘anonymous function’ refer to the same concept: functions that are values. Closures refer to a related, but different concept: the lifetime of a variable that is ‘closed over’, or in PHP 5.3 use‘d, by a closure, is bound to the lifetime, or extent, of the closure. Anonymous functions that are constructed with the use keyword are also closures. As mentioned, in PHP 5.3, anonymous functions are typed as Closure and the three words have, so far, been thrown about. The high-order bit to take away is an understanding that PHP 5.3 now includes language features for anonymous functions and closures.