views:

54

answers:

5

I would like to do something like:

function func($var1 = 'Hello', $var2 = "{$var1} World") { echo $var2; }

This is not possible!

My idea is to avoid extra code lines inside my function but I don't know if that is possible.

Does someone know if a similar procedure is possible in php? Thanks.

A: 

There is no expression language in PHP, but you can use the operator . to concatenate Strings.

Try this:

function func($var1 = 'Hello', $var2 = ' Worls') { echo $var1.$var2; }

No, you can't use it in the function parameter. Why would you want that? Just do it in the body function. It's better if there is no logic in the function parameters. More clear, more legible, more maintenable.

Also is not nice in terms of performance. You are creating a depencency between $var1 default value and $var2 default value. The processor could be assigning the default values in reverse order for whatever reason (mostly optimization) but since you introduced a dependency, it cannot be done any more, so the processor has to wait until $var1 is resolved to be able to assign $var2. Get it?

Anyway, the language thas not allow you to do it. But at least now you can have a possible reason of why (totally my assumption, I'm sure there are more different reasons for it).

pakore
I know that the operator . can concatenate Strings but you can't use that in function parameter.
Katie
+1  A: 

If you just want $var2 to be prepended by $var1 then you could just set it on the first line of the function eg.

function func($var1 = 'Hello', $var2 = " World") { 
    $var2 = $var1 . $var2;
    echo $var2; 
}
Matt Fellows
I know I can do something like that but I would like to avoid to inset code line in my function and I asked me if that is possible.
Katie
+2  A: 

Like the other answers say, you can't do this directly in PHP. My solution to these situations is usually to mimic the behaviour using something like this

function func($var1 = 'Hello', $var2 = null)
{
  if ($var2 === null)
    $var2 = $var1.' Worls';
}

But the big downside is you're copying PHP behaviour so you'll have to throw your own exceptions when variables are forgotten, the wrong type etc.

Michael Clerx
I wanted just avoid if statement, but maybe that is not possible.
Katie
+2  A: 

In php itself it is not possible (please correct me if i am worng). What you want to do is called aspect oriented programming. BUT i don't know any acceptable Aspect framework for php. For example in java you can use AspectJ and run code before the call of a certain MyClass.method(); so that everytime you call MyClass.method(); right before it is executed some other code will be executed automaticly wihtout adding it to the method() method itself. this is being achived by automaticly inserting code at the right point.

ITroubs
+1  A: 

I think the basic answer here is no, it isn't possible to do the way you're asking.

The closest you're going to get to the kind of functionality you're asking for is to use func_get_args() and related functions (although as has been stated in other answers, your specific use-case really only needs a couple of lines of pretty simple code at the top of your function).

Spudley