Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# out keyword meaning and does it have an equivalent in PHP? [duplicate]

Tags:

c#

php

out

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)
like image 702
StackOverflowNewbie Avatar asked Mar 21 '13 23:03

StackOverflowNewbie


2 Answers

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.

like image 72
dynamic Avatar answered Sep 28 '22 08:09

dynamic


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.

like image 32
Jason Watkins Avatar answered Sep 28 '22 07:09

Jason Watkins