Solved

anonymous function, closure

Posted on 2014-03-31
3
351 Views
Last Modified: 2014-04-01
from example #3
http://www.php.net/manual/en/functions.anonymous.php

<?php
// A basic shopping cart which contains a list of added products
// and the quantity of each product. Includes a method which
// calculates the total price of the items in the cart using a
// closure as a callback.
class Cart
{
    const PRICE_BUTTER  = 1.00;
    const PRICE_MILK    = 3.00;
    const PRICE_EGGS    = 6.95;

    protected $products = array();
    
    public function add($product, $quantity)
    {
        $this->products[$product] = $quantity;
    }
    
    public function getQuantity($product)
    {
        return isset($this->products[$product]) ? $this->products[$product] :
               FALSE;
    }
    
    public function getTotal($tax)
    {
        $total = 0.00;
        
        $callback =
            function ($quantity, $product) use ($tax, &$total)
            {
                $pricePerItem = constant(__CLASS__ . "::PRICE_" .
                    strtoupper($product));
                $total += ($pricePerItem * $quantity) * ($tax + 1.0);
            };
        
        array_walk($this->products, $callback);
        return round($total, 2);
    }
}

$my_cart = new Cart;

// Add some items to the cart
$my_cart->add('butter', 1);
$my_cart->add('milk', 3);
$my_cart->add('eggs', 6);

// Print the total with a 5% sales tax.
print $my_cart->getTotal(0.05) . "\n";
// The result is 54.29

Open in new window



I am having difficulty understanding code
and on call stack there is a cart->closure

cart closure. Where did it come from
0
Comment
Question by:rgb192
3 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 39968006
A closure is just an internal name for a more complex anonymous function. Think of a lambda as a very, very simple anonymous function that doesn't really have many moving parts inside of it (like a basic function that just adds a number or displays a value), while a closure is an anonymous function that has more functionality inside of it.

So to answer your question, the closure is just the special name for the anonymous function in that code example (the one assigned to $callback).
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39969104
No points for this, please, but if you find yourself wondering where you can use an anonymous function, feel free to stop wondering and just define a named function or method.  You'll be in good company.  Some language constructs are solutions in search of a problem.
0
 

Author Closing Comment

by:rgb192
ID: 39970834
Ray wrote no points.

I will move on to next code and not focus on anonymous.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question