[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

I do not understand calling sale function of new product

Posted on 2014-03-25
6
Medium Priority
?
324 Views
Last Modified: 2014-03-30
<?php
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


closures.php
<?php

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 ) );
0
Comment
Question by:rgb192
  • 4
  • 2
6 Comments
 
LVL 35

Expert Comment

by:gr8gonzo
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.
0
 

Author Comment

by:rgb192
ID: 39956570
Calling 2 methods at the same time
0
 

Author Comment

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

Sale method of new product
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 35

Accepted Solution

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

Author Comment

by:rgb192
ID: 39964987
okay same output but easier to understand

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



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
0
 

Author Closing Comment

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

Second question about this method is:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28400934.html
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses
Course of the Month18 days, 15 hours left to enroll

834 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