Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

php is flexible and loosely-typed. What does this mean. Give example

In reference to
<?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

Open in new window



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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

<?php
// Pre-defined function
function foo($bar)
{
   echo $bar + 1;
}

// Code that calls foo every time
foo(5); // Shows: 6

// Dynamically-call the function with the name "foo":
call_user_func("foo", 5); // Shows: 6


// Anonymous function
$foo_function = create_function('$bar','echo $bar + 1;');

// Dynamically-call the lambda function:
call_user_func($foo_function, 5); // Shows: 6

Open in new window



does anonymous mean calling without object?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

final answer compares anonymous to usort and uasort which I am familiar.  uasort(function,value)

Thanks