Solved

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

Posted on 2014-03-31
6
976 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 108

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 108

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now