6

I'm trying to convert this C# method into PHP. What does the out in the second parameter mean?

public static bool Foo(string name, out string key)
Mr.Me
  • 9,192
  • 5
  • 39
  • 51
StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441
  • First of all, I wouldn't know why you'd use a out-parameter. All out and ref parameters can be replace by simple returns. This provides more control of the output. – BlueCacti Mar 21 '13 at 23:19
  • @GroundZero ...it already returns another type. – J. Steen Mar 21 '13 at 23:20
  • @J.Steen Then use an object to return ... If you have a method which should return a first and a last name, use a Name object which included these 2 strings. This solves the ugly code using an out and a return. First of all, using out/ref should be avoided and you should definitly try avoiding both out/ref and return – BlueCacti Mar 21 '13 at 23:31
  • @GroundZero: Int.TryParse is a good example when out can be a good option. However, the method could indeed return a type that would put together the bool and the int variables, but the former approach seems more right here. – Luis Filipe Mar 21 '13 at 23:32
  • @GroundZero And if you're doing, say, a TryParse method? Then it's not really a good choice of solution. It's a matter of taste and convention, really. =) – J. Steen Mar 21 '13 at 23:32
  • 1
    I don't think this is a dup of that question. He asked how to transate out to php – dynamic Mar 25 '13 at 01:19
  • You cannot just return something when you are not sure that you will find it. Returning null is a bad design decision and so the best way to do this is to set desired output as parameter to a function and it's return make a boolean that will indicate whatever data is found or not. (answers to previous comments) – Kemsikov Jul 30 '22 at 05:43

3 Answers3

6

Documentationref

public static function foo($str, &$key)
                                 ^
                                 | Pass by reference

Please consider that in C# you must set a value in the calling method when using out and sadly you can't directly translate that into PHP.

dynamic
  • 46,985
  • 55
  • 154
  • 231
  • 1
    @MadaraUchiha: he asked how to convert that to PHP... And it's pretty self explanatory I believe. – dynamic Mar 21 '13 at 23:13
  • 1
    @llnk: If it would be self explanatory he wouldn't aks the qustion and you wouldn't equate `ref` with `out` ;-) – Tim Schmelter Mar 21 '13 at 23:14
  • It's not self explanatory. The question *mainly* asks about what does the `out` keyword mean. The conversion is secondary. *also*, as someone who doesn't know C#, I have no idea what the `out` parameter mean, so your question has (had) little value to me. – Madara's Ghost Mar 21 '13 at 23:14
  • @Madara: I have wrote pass by reference, I believe pass by reference is self explanatory... don't you think? – dynamic Mar 21 '13 at 23:19
  • @llnk: Only from what I see, `out` != `pass by reference` or is it? I can remove my downvote after that last edit. But look at your first revision and think hard on whether my downvote was unjustified. – Madara's Ghost Mar 21 '13 at 23:21
  • 1
    @MadaraUchiha: Ok I appreciate constructive discussion :) – dynamic Mar 21 '13 at 23:21
5

The out keyword specifies that the parameter must be assigned to by the called method, and the value assigned will be passed back to the calling method. Check out MSDN for more info.

I don't think PHP has an equivalent for the required assignment behavior, but if you are converting the method body, and maintain that behavior internally, you should be able to convert it to a regular pass by reference parameter and maintain the same functionality.

Jason Watkins
  • 3,766
  • 1
  • 25
  • 39
1

A tiny bit of Googeling brought me to this site: http://www.php.net/manual/en/language.references.pass.php

As you know, a parameter is a copy of a variable. This means you won't actually change the variable itself.
For example:

<?php
    function foo($bar) {
        $bar++;
    }

    $bar = 5;  // $bar = 5
    foo($bar); // $bar = 6

    echo $bar; // $bar = 5
?>

Whilst this piece of code will actually change the given variable, as is uses reference.

<?php
    function foo(&$bar) {
        $bar++;
    }

    $bar = 5;  // $bar = 5
    foo($bar); // $bar = 6

    echo $bar; // $bar = 6 now
?>

NOTE: this is not an exact PHP-version of the out-parameter you have in C#

BlueCacti
  • 9,729
  • 3
  • 20
  • 24