Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

anonymous function, closure

Posted on 2014-03-31
3
Medium Priority
?
364 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 35

Accepted Solution

by:
gr8gonzo earned 2000 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 111

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

783 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