Archived

This forum has been archived. Please start a new discussion on GitHub.

php ice interface struct initialization

Hi,
Is there a way to add a constructor to initialize a struct instance. I need to pass this as a parameter as shown in the code fragment.

thanks,
Don

// section from CU.ice:
module CU
{
// Transaction, becomes CU_Transaction
struct Transaction {
string posteff;
int account;
...
}
bool transfer(Transaction from, Transaction to) throws PostError;
}

// testing usage of transfer from PHP
$xferfrom = array ("posteff" => "2006-05-15","account" => (int)12345, "suffix" => (int)51, "accttype" => "S","mnemonic" => "SDT", "amount" => (double)12345.67);

$fromshr = new CU_Transaction($xferfrom);
// constructor doesn't work, so initialize the hard way
$fromshr->posteff = '2006-05-16';
$fromshr->account = 2;
$fromshr->accttype = 83;
$fromshr->suffix = 1;
$fromshr->mnemonic = 'SWX';
$fromshr->amount = 100;
$fromshr->descr = 'xfer test from php';
// now post the transfer
$ok = $gcu->transfer($fromshr, $toshr);
// it works!, PHP and Ice are Great!

Comments

  • mes
    mes California
    Hi Don,

    The Slice-to-PHP mapping doesn't currently define a "one shot" constructor that accepts values for all data members. We probably should, but I don't think it would do what you want. It looks like what you want is a constructor that accepts an array containing values for each of the data members. This is something you could do yourself fairly easily:
    $fromshr = new CU_Transaction;
    foreach($xferfrom as $key => $value)
        $fromshr->$key = $value;
    
    Hope that helps,
    - Mark