Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# anonymous function, closure

Posted on 2014-03-31
Medium Priority
360 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
``````

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

0
Question by:rgb192
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
3 Comments

LVL 35

Accepted Solution

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

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

ID: 39970834
Ray wrote no points.

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

## Featured Post

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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
Course of the Month4 days, 13 hours left to enroll

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

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