0

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.

2
  • Is that to actually TEST that the second parameter "contains" the first one? In that case, I wish we all had pattern matching in PHP, but this is unfortunately not the case... Commented Oct 5, 2010 at 12:42
  • That is to assign the first parameter value plus something to the second one. I think it doesn't make sense the pattern matching in this case. Commented Oct 5, 2010 at 13:35

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I wanted just avoid if statement, but maybe that is not possible.
2

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.

Comments

1

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; 
}

1 Comment

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.
1

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).

Comments

0

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).

1 Comment

I know that the operator . can concatenate Strings but you can't use that in function parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.