Searching...
Saturday 29 June 2013

Functions in php – Part 3


Using Type Hinting

PHP 5 introduced a new feature known as Type hinting, which gives you the ability to force parameters to be objects of a certain class or to be arrays. Unfortunately, type hinting using scalar data types such as integers and strings is not supported. If the provided parameter is not of the desired type, a fatal error will occur. As an example, suppose you created a class named Customer and wanted to be certain that any parameter passed to a function named processPayPalPayment() was of type Customer. You could use type hinting to implement this restriction:

function processPayPalPayment(Customer $customer) {
// Process the customer's payment
}


Returning Values from a Function

Often, simply relying on a function to do something is insufficient; a script’s outcome might depend on a function’s outcome or on changes in data resulting from its execution. Yet variable scoping prevents information from easily being passed from a function body back to its caller, so how can we accomplish this? You can pass data back to the caller by way of the return() statement.

The return Statement

The return() statement returns any ensuing value back to the function caller, returning program control back to the caller’s scope in the process. If return() is called from within the global scope, the script execution is terminated. Revising the calcSalestax() function again, suppose you don’t want to immediately echo the sales total back to the user upon calculation, but rather want to return the value to the calling block:

function calcSalesTax($price, $tax=.0675)
{
$total = $price + ($price * $tax);
return $total;
}

Alternatively, you could return the calculation directly without even assigning it to $total, like this:

function calcSalesTax($price, $tax=.0675)
{
return $price + ($price * $tax);
}

Here’s an example of how you would call this function:

<?php
$price = 6.99;
$total = calcSalesTax($price);
?>

To return a reference from a function, use the reference operator & in both the function declaration and when assigning the returned value to a variable(more about variables in php):

Example #3 Returning a reference from a function

<?php
function &returns_reference()
{
    return $someref;
}
$newref =& returns_reference();
?>

Returning Multiple Values

It’s often convenient to return multiple values from a function. For example, suppose that you’d like to create a function that retrieves user data from a database (say the user’s name, e-mail address, and phone number) and returns it to the caller. Accomplishing this is much easier than you might think, with the help of a very useful language construct, list(). The list() construct offers a convenient means for retrieving values from an array, like so:

<?php
$colors = array("red","blue","green");
list($red, $blue, $green) = $colors;
?>

Once the list() construct executes, $red, $blue, and $green will be assigned red, blue, and green, respectively.
Building on the concept demonstrated in the previous example, you can imagine how the three prerequisite values might be returned from a function using list():

<?php
function retrieveUserProfile()
{
$user[] = "Jason Gilmore";
$user[] = "jason@example.com";
$user[] = "English";
return $user;
}
list($name, $email, $language) = retrieveUserProfile();
echo "Name: $name, email: $email, language: $language";
?>

Executing this script returns the following:

Name: Jason Gilmore, email: jason@example.com, language: English

This feature is quite useful and will be used repeatedly throughout your journey in php programming.


0 comments:

Post a Comment

 
Back to top!