rgb192
asked on
what goes into registerCallback lines 20,22
I know line24 can be replaced with
$temporary_variable=new Product("shoes",6);
$processor->sale($temporar y_variable );
I created
$first_object
$second_object
because I want to know what goes into registerCallback lines 20,22
listing4.15
closures.php
output
$temporary_variable=new Product("shoes",6);
$processor->sale($temporar
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 ) );
?>
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 );
}
}
}
?>
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
so you'd need to do:
try {
$processor->registerCallba ck($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.
try {
$processor->registerCallba
}
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.
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!
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!
ASKER
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->registerCallba
I want to understand what //$processor->registerCall
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
could you explain why and other examples of what process sale could do?
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
or simply
it dumps as-is whatever is an argument, regardless. at least you know where you are atm :)
print_r(array( new Mailer(), "doMail" ));
it dumps as-is whatever is an argument, regardless. at least you know where you are atm :)
ASKER
<?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 ) );
?>
Fatal error: Uncaught exception 'Exception' with message 'callback not callable' in C:\wamp\www\POPP-edition4-
when
//$processor->registerCall
is uncommented no exception is thrown
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
$processor->registerCallba ck($x);
works
thanks
works
thanks
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->registerCallba
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.