Jump to content

A question about effective parameters names mapping to formal parameters names during function call in PHP


dariyoosh
Go to solution Solved by kicken,

Recommended Posts

Dear all,

 

I have a question about function call in PHP and I would appreciate if you could kindly make some clarification. Generally, I prefer to call my functions by name, that is, instead of writing

functionName(paramValue, paramValue, . . .)

I prefer to provide the name of the formal parameters as they were specified in the function prototype.

functionName(param1Name=paramValue, param2Name=paramValue, . . .)

Well, maybe it's just a matter of personal taste, but It seems to me more elegant and in particular for someone who reads my code might be more clear and easier to understand what I provide exactly in terms of parameters while calling the function. And this could be even more helpful, particularly if the function includes so many arguments among them several with default values.

 

Yet, I've just found (I'm Newbie :)) that PHP, actually doesn't care about the fact that the name of the effective parameters are different of those of formal parameters. Consider the following example

 

function myFunction($param1, $param2)
{
    echo "param1 = " . $param1 . "<br>";
    echo "param2 = " . $param2 . "<br>";
}

myfunction
(
    $param1=300, 
    $param2="id-01001GGKK"
);

As you can see in this example the name of the effective parameters in the function call correspond to those specified in function prototype, resectively, param1 and param2.

Yet, I tested successfully the following

myfunction
(
    $size=120, 
    $code="id-010012F"
);

Although the name of the parameters were completely different, again the function was executed successfully without any problem.

 

Is that normal?

 

In addition, the only way that I found to enforce the parameters names was to use an associative array including the list of parameters. Something like this:

 

function myFunction($paramArray)
{
    if (isset($paramArray["param1"]) AND
        isset($paramArray["param2"]))
    {
        echo "param1 = " . $paramArray["param1"] . "<br>";
        echo "param2 = " . $paramArray["param2"] . "<br>";
    }
    else
        die("Bad argument list");
}

Yet, it's not clear. I would like that each parameter name be visible.

 

So, I would like to ask, is there any other way to enforce the effective parameter names to be the same as those specified in the function prototype?

 

One other reason I ask this question is that until now, whenever I wanted to call a built-in PHP function, I used to check its documentation in order to first understand its functionality and second to view in detail its parameter list, formal parameter names to use them in my code. And I was thinking that I was doing fine and PHP takes them into account!! :(

 

Thanks in advance,

 

 

Regards,

Dariyoosh

Edited by dariyoosh
Link to comment
Share on other sites

  • Solution

Is that normal?

Yes. What is happening is not what you think is happening most likely. PHP does not support any sort of name-based parameters when calling a function. It is all handled by position and only position. What you're doing in that code is simply assigning the value to a variable prior to passing it into the function. It's functionally the same as just doing something like:
$size = 120;
$code = "id-010012F";
myfunction($size, $code);
There's nothing really wrong with doing what you are doing (just be careful to not over-write needed variables) but there is nothing in PHP that is going to require the names match each other. As far as PHP is concerned the parameters do not have any names, just a position.

So, I would like to ask, is there any other way to enforce the effective parameter names to be the same as those specified in the function prototype?

Yes, passing an associative array would be the only way you could require that the names a function uses internally matches what a user passes into it.
Link to comment
Share on other sites

. . .Yes, passing an associative array would be the only way you could require that the names a function uses internally matches what a user passes into it.

Hello there,

 

Thank you very much for this nice and clear description. Just one more question (as I'm newbie :)) which method among the following is more common and is considered to be a good style of programming? (so that I follow it in the future)

 

1 - Simply call the function (according to the position of the parameters) by directly providing the value

2 - Call the function by writing

functionName(paramName1 = value1, paramName2=value2, . . .)

Although that according to what you said, PHP takes into account only the position (just to make the code more explicit for the reader)

3 - using an associative array

 

Thanks a lot,

 

Regards,

Dariyoosh

Link to comment
Share on other sites

... is more common and is considered to be a good style of programming? (so that I follow it in the future)

 

1 - Simply call the function (according to the position of the parameters) by directly providing the value

This is certainly the most common way of calling a function. It is perfectly acceptable as far as style and good practices go also. As you learn the language (or libraries) you will start to remember what parameters functions accept and in what order. When you do forget, there is always the manual also, just look it up. IDE's help a lot in this area by either showing you the function signature as you type or providing a quick link to such information.

 

 

 

2 - Call the function by writing

 

functionName(paramName1 = value1, paramName2=value2, . . .)

Although that according to what you said, PHP takes into account only the position (just to make the code more explicit for the reader)

I don't think I've ever seen anything like this done really. The extra documentation is generally not necessary. I most cases you'd probably be passing variables anyway as opposed to constant values. So long as the variables are named well things will be understood. There is a probably a good chance of having variable conflicts/problems also if you start "naming" all your parameters when calling the function.

3 - using an associative array

This is often useful for functions which accept a large number of optional parameters. Rather than having to type out a bunch of defaults for parameters you don't want to specify you can just pass an array with the correct keys defined.

 

Take a look for example at the Image function in TCPDF. Say you wanted to set the ALT text and BORDER on an image but don't care about all the rest of those parameters. To do that you end up with a function call looking like:

 

$pdf->Image($file, $x, $y, $w, $h, '', '', '', false, 300, '', false, false, $border, false, false, false, $alt);

 

Quite a nightmare to read, and unless you are very familiar with that library you'll be constantly looking up such functions in the manual. Using an array for all those optional parameters would make things a lot nicer:

$pdf->Image($file, $x, $y, $w, $h, array(
'border' => $border
, 'alt' => $alt
));
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.