Solved

what goes into registerCallback lines 20,22

Posted on 2014-04-23
9
215 Views
Last Modified: 2014-04-25
I know line24 can be replaced with
$temporary_variable=new Product("shoes",6);
$processor->sale($temporary_variable);


I created
$first_object
$second_object

because I want to know what goes into registerCallback lines 20,22


listing4.15
<?php
require_once( "closures.php" );

class Mailer {
    function doMail( $product ) {
        print "    mailing ({$product->name})\n";
    }
}

class Delivery {
  function doDelivery($product ){
      print "<h1>Free shipping for {$product->name}, you still pay {$product->price}</h1>";
  }
}

$processor = new ProcessSale();
$first_object=new Mailer();
$first_object->doMail();
$second_object=new Delivery("doDelivery");
//$processor->registerCallback( array( new Mailer(), "doMail" ) );
$processor->registerCallback($first_object);
$processor->registerCallback( array( new Delivery(), "doDelivery" ) );

$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


output
Warning: Missing argument 1 for Mailer::doMail(), called in C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_04_Code\listing4.15.php on line 18 and defined in C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_04_Code\listing4.15.php on line 5

Notice: Undefined variable: product in C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_04_Code\listing4.15.php on line 6

Notice: Trying to get property of non-object in C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_04_Code\listing4.15.php on line 6
mailing () 
Fatal error: Uncaught exception 'Exception' with message 'callback not callable' in C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_04_Code\closures.php:17 Stack trace: #0 C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_04_Code\listing4.15.php(21): ProcessSale->registerCallback(Object(Mailer)) #1 {main} thrown in C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_04_Code\closures.php on line 17

Open in new window

0
Comment
Question by:rgb192
  • 4
  • 3
  • 2
9 Comments
 
LVL 11

Expert Comment

by:Radek Baranowski
ID: 40019626
well what you do is creating new object of Mailer type as $first_product

Mailer per se is not callable (it's not a function). only doMail() is a function that can be called.

$first_object=new Mailer();
$first_object->doMail();

so if you pass Mailer object to here:

$processor->registerCallback($first_object);

the code:

function registerCallback( $callback ) {
        if ( ! is_callable( $callback ) ) {
            throw new Exception( "callback not callable" );

throws you an exception (as you desire) , which is not caught anywhere in your code (remember, if you willingly throw an exception, then you need to catch it properly too - http://www.php.net/manual/en/language.exceptions.php) - hence your code stops and raises an error.
hope this helps.
0
 
LVL 11

Expert Comment

by:Radek Baranowski
ID: 40019632
so you'd need to do:

try {
$processor->registerCallback($first_object);
}
catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
and your programme will go on without stopping.

however, I'm not sure what exactly you are trying to achieve, so that's just explanation you asked for.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40019932
The overall design of what I see here looks like this (in plain language).

Class Product stores a name and a price.

Class ProcessSale has the ability to call a varying number and type of functions when the sale() method is called.  These functions are not enumerated and predefined by the sale() method. Instead they are registered at run-time by calling the registerCallback() method.

The registerCallback() method expects its input to be a callable function or method of an object.  The syntax for functions and object methods is slightly different in is_callable().

PHP is_Callable() has some flaky things about its implementation.  Example here.  I would advise you to read the entire collection of user-contributed notes carefully.  You may find that this is too quirky and complicated for use in any practical implementation!
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

Author Comment

by:rgb192
ID: 40021784
throws you an exception (as you desire)
that is the code I had and I do not want to throw exception


$first_object=new Mailer();
$first_object->doMail();


$processor->registerCallback($first_object);

I want to understand what //$processor->registerCallback( array( new Mailer(), "doMail" ) ); is so I can do a var_dump

I want a var_dump of array( new Mailer(), "doMail" )
I want this as a variable



Class Product stores a name and a price.
this is all I understand


Class ProcessSale has the ability to call a varying number and type of functions when the sale() method is called.  These functions are not enumerated and predefined by the sale() method. Instead they are registered at run-time by calling the registerCallback() method.
could you explain why and other examples of what process sale could do?
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 350 total points
ID: 40022381
This is (programmatically) nothing because it is a PHP comment:

 //$processor->registerCallback( array( new Mailer(), "doMail" ) );

Maybe this would help?

$x =array( new Mailer(), "doMail" );
var_dump($x);
0
 
LVL 11

Expert Comment

by:Radek Baranowski
ID: 40022384
or simply

print_r(array( new Mailer(), "doMail" ));

Open in new window


it dumps as-is whatever is an argument, regardless. at least you know where you are atm :)
0
 

Author Comment

by:rgb192
ID: 40022396
<?php
require_once( "closures.php" );

class Mailer {
    function doMail( $product ) {
        print "    mailing ({$product->name})\n";
    }
}

class Delivery {
  function doDelivery($product ){
      print "<h1>Free shipping for {$product->name}, you still pay {$product->price}</h1>";
  }
}

$processor = new ProcessSale();
$first_object=new Mailer();
//$first_object->doMail();
$second_object=new Delivery("doDelivery");
$x=array($first_object,"doMail");
//$processor->registerCallback( array( new Mailer(), "doMail" ) );
$processor->registerCallback($first_object);
$processor->registerCallback( array( new Delivery(), "doDelivery" ) );

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

Open in new window



Fatal error: Uncaught exception 'Exception' with message 'callback not callable' in C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_04_Code\closures.php:17 Stack trace: #0 C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_04_Code\listing4.15.php(22): ProcessSale->registerCallback(Object(Mailer)) #1 {main} thrown in C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_04_Code\closures.php on line 17



when
//$processor->registerCallback( array( new Mailer(), "doMail" ) );
is uncommented no exception is thrown
0
 
LVL 11

Accepted Solution

by:
Radek Baranowski earned 150 total points
ID: 40022418
you mean changing to this :

$processor->registerCallback( array( new Mailer(), "doMail" ) );
//$processor->registerCallback($first_object);

Open in new window


?

yes it's valid then because if you pass just an object to is_callable, it's not true, but If you pass object and method it yields true.

check the example #1 here: http://www.php.net/manual/en/function.is-callable.php
0
 

Author Closing Comment

by:rgb192
ID: 40022433
$processor->registerCallback($x);
works

thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
comma true 6 37
Loop through all records or search for one specific record when checking if email exists 6 37
Wordpress Query 5 24
issue with DB import 1 17
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 is …
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.
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 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…

792 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