Solved

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

Posted on 2014-03-31
6
1,011 Views
Last Modified: 2014-04-01
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.
0
Comment
Question by:rgb192
  • 2
  • 2
  • 2
6 Comments
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 366 total points
ID: 39967528
"Flexible" is not really a term of art -- it just describes the language as being rich with many ways to accomplish the same end result.

"Loosely typed" is a term of art.  These references should help clear it up.  Without repeating what you will be able to bookmark in the references, I'll just summarize by saying that PHP is sometimes called a "duck type" language.  If it walks like a duck and quacks like a duck, it must be a duck.  In PHP if it can be used like an integer it must be an integer.  If it can be used as a string, it must be a string, etc.  PHP will juggle the data types to fit the context in which they are used.  Many languages will not do this, and if you want to get the printed representation of the integer 3, you must explicitly write a conversion routine to turn the integer 3 into the printable string "3" so that it can be displayed.  Such languages are called "strictly typed."
http://www.php.net/manual/en/language.types.php
http://www.php.net/manual/en/language.types.type-juggling.php
http://www.php.net/manual/en/types.comparisons.php
0
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 134 total points
ID: 39967547
Loosely-typed simply means that variables and parameters can change into different types. For example:

$x = "1"; // $x is a string "1"
$x = $x + 1; // Now $x is a number 2

Open in new window


This is allowed because PHP will try to automatically figure out what you want to do with your code and will try to convert the variables automatically for you. So it tries to convert $x to a number and then it tries to add 1 to that number.

In many compiled languages, such as the .NET languages, things are strongly-typed, so once they start as one type of variable, they cannot change:

(C# example)
string x = "1";
x = x + 1; // <--- this will create an error and prevent the program from running, because x is a string and you cannot perform math on a string.

Open in new window


There are advantages and disadvantages, but those are largely based around your application. Sometimes it is better or easier for an application to be loosely-typed and sometimes it is better to be strongly-typed.

Strongly-typed languages tend to require more code at the beginning because the code flows in a very specific way every time you run it. Loosely-typed languages are more flexible, but they will usually run slower because the engine has to watch and test for changes rather than simply knowing how everything will go.

That said, you can often write functions in PHP that can handle different types of parameters and change the code flow easily, so you often don't up needing to create lambda functions to handle specific situations in your code.

In addition, PHP is fairly flexible when it comes to any kind of dynamic flows or calling functions in a dynamic manner. For example:

// 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

Open in new window


Now, you COULD create a lambda/anonymous function for this and store a reference to it and call it that way, too:

// 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


However, there's not a lot of benefit to using the lambda function here, and the lambda function will perform -slightly- worse than the predefined function. Also, it is harder to maintain (because you have to write the code inside of a string).
0
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 134 total points
ID: 39967566
There's also something to be said for the language presenting opportunities to use different types of coding approaches. For example, C# has an extension called Linq that works with data and arrays and such. Linq provides many methods for user-defined sorting of data, and lambda methods tend to work best in those scenarios. Plus, C# can see the lambda functions in advance and create compiled versions of them so they execute better at runtime.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:rgb192
ID: 39967736
<?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?
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 366 total points
ID: 39967772
Anonymous functions do not have names.
http://www.php.net/manual/en/functions.anonymous.php

This is a good example.  Instead of having to create a separate (named) function, you can build the function right into the uasort() statement.
http://www.php.net/manual/en/functions.anonymous.php#91871
0
 

Author Closing Comment

by:rgb192
ID: 39970843
final answer compares anonymous to usort and uasort which I am familiar.  uasort(function,value)

Thanks
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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.
The viewer will learn how to dynamically set the form action using jQuery.
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…

821 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