Jump to content

function arguments that act as return values?


ultrus

Recommended Posts

Howdy,
I'm attempting to convert a delphi script into the php equivelent as I'm using php to communicate with an external server. I don't know much about delphi, but it seems that arguments in a called function are actually returning values like this:

[code]ServerObject.GetSettings('speed', current, default, min, max);[/code]

'speed' is what is passed in, and the remaining arguments return something like this:

[code]ShowMessage(Format('Current: %d, Defaut: %d, Min: %d, Max: %d', [current, default, min, max]))[/code]

Can I do something like this in php? I have tried the following without success:

[code]$settingsSpeed = $serverObject->GetSettings("speed", $current, $default, $min, $max);
echo($min);[/code]

Any ideas on this? Thanks much.
Link to comment
Share on other sites

[s]If you're asking how to define the default for an argument, it's
function foo($bar="x"){
}

If not, please rephrase the question, I don't get it.[/s]

Okay I think I get it now, but I don't know how to do what you want without seeing more of your code. What is the serverobject class? Is speed a var of it? In your getsettings function, you'd have to handle getting the attributes.
Link to comment
Share on other sites

[s]nah. I found that as well. When the function is called, the server is supposed to return the $current, $default, $min, $max values of "speed" or whatever other string passed in. In php I'm used to just retirning one value from a function, but this function returns variables to the variable names you specify as arguments. $current, $default, $min, and $max are null before the function is used. Please excuse my lack of technical terms.[/s]

printf($settingsSpeed) and Format($settingsSpeed) don't cause any errors, but still draw a blank. This is the only challenge I have. I'm able to make use of all other server functions from php. Thanks for the assist.  :)
Link to comment
Share on other sites

$settingsSpeed = $serverObject->GetSettings("speed", $current, $default, $min, $max);
echo($min);

What does this code do? Do you know what GetSettings does? Are those even valid arguments?
I thought you said you were converting it - if you're writing it, can't you change what GetSettings does?

PHP passes by value - try passing those by reference? &$min
Otherwise $min is not affected by the function, because the $min in the function is a copy of $min. (confused yet?)
Link to comment
Share on other sites

I'm not clear on what you want. Something like this perhaps?

[code]
<pre>
<?php
function test ($something, $current, $default, $min, $max) {
printf('Current: %d, Defaut: %d, Min: %d, Max: %d', $current, $default, $min, $max);
}
test('speed', 1, 2, 3, 4);
?>
</pre>

[/code]
Link to comment
Share on other sites

jesirose, effigy,
Sorry for the lack of clearification. I abreviated the script for forums simplicity. I'm actually converting Delphi and Visual Basic [i]usage[/i] examples into php usage examples. I'm communicaing with text to audio conversion software using the Microsoft COM object. The full Delphi example script looks like this:

[code]procedure TForm1.DisplaySpeedSettings(Voice: String);
var
  Currrent: Integer;
  Default: Integer;
  Min: Integer;
  Max: Integer;
begin
  TextAloudObject.GetVoiceSettings(Voice, 'speed', current, default, min, max);
  ShowMessage(Format('Current: %d, Defaut: %d, Min: %d, Max: %d', [current, default, min, max]))
end;[/code]

Voice is a string specified earlier on. 'speed' is a string that can also be 'volume' or 'pitch'.

I think I understand the reasoning behind the reference. I'm not sure on usage.


I'm trying to read the current speed, default speed, minimum speed, and maximum speed from this other server using the function they provided.
Link to comment
Share on other sites

Pass by reference, use a class, use global vars, or return an array.

For the second line, printf/sprintf should work once you get the vars coming back in.

Example using a class:
[code]
<?php
class serverObject{
var $min;
function Getsettings($min) {
$this->$min = $min + 15;
}
}

$gs = new serverObject;
$gs->Getsettings(30);
echo $gs->min;

printf("Min: %d", $gs->min);[/code]

I can't really help more than that without seeing more code.
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.