Posted by: phillipnb | July 1, 2012

Wrapper Methods in PHP

A few days back I was accidentally drawn into an argument about what a wrapper method was? I have summarized that discussion here for the benefit of others.

As far as PHP is concerned, a wrapper method is any ordinary method that wraps an existing method to satisfy some business logic. For example: Let there be a method called ‘A’ and let there be another method called ‘B’ which uses ‘A’, produces an output and then manipulates that output to do something or produce a new output. Here, ‘B’ is called the wrapper method because it wraps around method A. We had to create the method B because method ‘A’ could satisfy our need only partially. Some people call method ‘A’ as the expert method and method ‘B’ as the slave or the wrapper method. Here the slave method asks the expert method a question, a question that the expert method understands. The answer given by the expert method is then manipulated or massaged by the wrapper method to produce the desired output.

Wrapper Method in PHP – An Example:

function sortThisArray($in_array)
    if (count($in_array) < 0)
    	echo "\n Looks like the array is empty!.";
    return $in_array;

function wrapper_sortAndReturnEven($in_array)
	$temp = sortThisArray($in_array);
	$new_array = array();
	foreach($temp as $val)
		if (bcmod($val,2) == 0)
			$new_array[] = $val;
	return $new_array;

$temp = array(5,6,2,9,1,3,10,4,7,8,15,12,11,14,13);

In the above example, sortThisArray is the expert method. You can see that the wrapper method wrapper_sortAndReturnEven uses sortThisArray, gets an answer and then manipulates it to produce the desired output. So, wrapper method actually produces an alternative interface for an existing method. We had to write wrapper_sortAndReturnEven because sortThisArray could not fully satisfy our business requirement. Now you know what a wrapper method IS?.

So, that is about Wrapper Methods or Wrapper functions. Nothing more to let you know from the PHP world except that PHP 5.4.4 was released recently.

As usual, I welcome your comments – You can use the comments section of this blog.

So, till next time, it will be Happy PHP Programming.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: