rgb192
asked on
php is flexible and loosely-typed. What does this mean. Give example
In reference to
php is flexible and loosely-typed. What does this mean. Give example
<?php
class Salesperson
{
private $special_boss_requests=array();
public function add_boss_requests($request)
{
//add the request to the list of things to do
$this->special_boss_requests[]=$request;
}
function sale($item,$credit_card,$buyer)
{
$this->scan_item($item);
$receipt = $this->swipe_credit_card($credit_card);
$this->give_receipt_and_item($receipt, $item, $buyer);
//now perform all the boss's special requests, passing the buyer's name to each one
foreach($this->special_boss_requests as $request){
call_user_func($request,$buyer);
}
}
function scan_item($item)
{
// scan the item
}
function swipe_credit_card($card)
{
// send credit card data and get a receipt back
return "receipt";
}
function give_receipt_and_item($receipt, $item, $buyer)
{
// give the stuff to the person who is buying the item
}
}
//start your day
$me=new Salesperson();
//you sell a pari of shoes to joe smith
$me->sale("shoes","joe smith credit card","Joe Smith");
//at this point you are not saying thank you yet
//the boss comes up with idea to say thank you which is our lambda function
$say_thank_you=create_function('$name','print "Thank you,{$name} for shopping with us!";');
//the boss tells you to do it
$me->add_boss_requests($say_thank_you);
//now when you perform a sale...
//sell coffee to mary smith
$me->sale("coffee", "mary smith credit card", "mary smith");
//after doing everying you say, thank you mary smith for shopping with us
//now the boss comes up with another idea-make sure you also tell them about our website
//the boss comes up with idea to say thank you which is our lambda function
$say_website_reminder=create_function('$name','print "Also{$name}, make sure you visit our website";');
//the obss tells you to do it
$me->add_boss_requests($say_website_reminder);
//at this point you now have $say_thank_you and say_website_reminder
$another_reminder=create_function('$name', 'print "and {$name}, also close the door on the way out";');
$me->add_boss_requests($another_reminder);
//sell a car to george smith
$me->sale("car", "george smith credit card","george smith");
//now george smith should hear both at the end
php is flexible and loosely-typed. What does this mean. Give example
To be honest, I RARELY ever use anonymous methods / lambda functions. PHP is generally so flexible and loosely-typed that the predefined functions can often do what you need and they perform better.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
final answer compares anonymous to usort and uasort which I am familiar. uasort(function,value)
Thanks
Thanks
ASKER
Open in new window
does anonymous mean calling without object?