Archived

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

Error creating structure

I am trying to create an instance of the following structure using PHP:

struct priceRequestInfo {
int productId;
string customer;
opt4ice::decimal quantity;
};

where opt4ice::decimal has the following definition:

struct decimal {
long intval;
short ndp;
};

The following code fails:

$id = 1;
$customer = "ABC";
$quantity = new opt4ice_decimal(100, 2);
$request = new priceRequestInfo($id, $customer, $quantity);

The error message for the above is "Object of class opt4ice_decimal could not be converted to int".

However the code below succeeds:

$id = 1;
$customer = "ABC";
$quantity = new opt4ice_decimal(100, 2);
$request = new priceRequestInfo($id, $customer, 0);
$request->quantity = $quantity;

If I just leave the last argument out (i.e. $request = new priceRequestInfo($id, $customer) ) then the structure is created correctly and the quantity attribute is a valid opt4ice::decimal. Is there a known problem passing a structure to the constructor of another structure or is this something new? Or am I just doing something wrong?

Comments

  • mes
    mes California
    Hi Andrew,

    Welcome to the forum!

    You didn't describe your environment (Ice version, operating system, PHP version, etc.), so I tried to reproduce this issue with Ice 3.3.1 on CentOS using the default PHP version (5.1.6). Everything works correctly with this combination.

    However, I can see where the problem might lie. Add a call to Ice_dumpProfile to your PHP script:
    <?php
    Ice_loadProfile();
    Ice_dumpProfile();
    ...
    
    This will cause the Ice extension to display all of the PHP code it generates from your Slice definitions. In the output you'll see the following:
    class opt4ice_priceRequestInfo
    {
    function __construct($productId=0, $customer='', $quantity=-1)
    {
        $this->productId = $productId;
        $this->customer = $customer;
        $this->quantity = $quantity == -1 ? new opt4ice_decimal() : $quantity;
    }
    
    The generated code is using the value -1 as a marker to determine whether the caller supplied a value for the argument. Apparently, the version of PHP you are using does not allow such a comparison.

    We'll fix this bug in the next Ice release. Meanwhile, you can work around it as you suggested: by omitting the quantity argument and assigning the member after construction.

    Let us know if you have any further questions.

    Regards,
    Mark
  • Mark

    Many thanks for your reply - that certainly makes sense given what I am seeing. I am running PHP 5.2.6 with Ice 3.3.0 on Ubuntu 9.0.4. It looks as though my version of PHP is trying to coerce $quantity to an integer to compare it to -1, which fails and generates the exception. Fortunately the work around is straightforward, but it's always nice to know what causes these things.

    Regards

    Andrew