I do not understand calling sale function of new product

Posted on 2014-03-25
Last Modified: 2014-03-30
require_once( "closures.php" );

$logger = create_function( '$product', 
                           'print "    logging ({$product->name})\n";' );

$processor = new ProcessSale();
$processor->registerCallback( $logger );

$processor->sale( new Product( "shoes", 6 ) );
print "\n";
$processor->sale( new Product( "coffee", 6 ) );

Open in new window


class Product {
    public $name;
    public $price;
    function __construct( $name, $price ) {
        $this->name = $name;
        $this->price = $price;

class ProcessSale {
    private $callbacks;

    function registerCallback( $callback ) {
        if ( ! is_callable( $callback ) ) {
            throw new Exception( "callback not callable" );
        $this->callbacks[] = $callback;

    function sale( $product ) {
        print "{$product->name}: processing \n";
        foreach ( $this->callbacks as $callback ) {
            call_user_func( $callback, $product );

Open in new window

I do not understand calling sale function of new product

$processor->sale( new Product( "shoes", 6 ) );
Question by:rgb192
  • 4
  • 2
LVL 34

Expert Comment

ID: 39955054
What specifically do you not understand about it?

To be clear, the sale function does not belong to the new product. The sale function belongs to the $processor object, and the code is creating a new product object and passing that new product as a parameter to the $processor's sale function.

Author Comment

ID: 39956570
Calling 2 methods at the same time

Author Comment

ID: 39962398
I do not understand calling two classes at the same time.

Sale method of new product
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

LVL 34

Accepted Solution

gr8gonzo earned 500 total points
ID: 39963127
This code:
$processor->sale( new Product( "shoes", 6 ) );

...could be rewritten as two lines:

$temporary_variable = new Product( "shoes", 6 );
$processor->sale( $temporary_variable );

The only difference is that the "new Product" part is created in memory and is passed immediately to the sale function and there is no $temporary_variable or other variable to reference it outside of the function.

Author Comment

ID: 39964987
okay same output but easier to understand

//$processor->sale( new Product( "shoes", 6 ) );
$temporary_variable=new Product("shoes", 6);

Next part of sale function I do not understand is the foreach
this run was value lambda_8, last run was lambda_8
Only one run in the foreach

what is lambda and why always run once

Author Closing Comment

ID: 39965428
Yes you answered the question about calling the method.
Thank you.

Second question about this method is:

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now