Home Help Center

Strange error in PHP

xoraxxorax Member Ivan BrykovOrganization: Opticsplanet inc
Hello everybody!

I'm currently stuck with Fatal PHP error that I cannot explain. Perhaps you guys can help me.

So.. in my .ice file there is a record in interface:
int apiTradesCalcProfit(string group, TradeRecord TrIn, out TradeRecord TrOut) throws ServerError;

I'm trying to call it from PHP like this:
[PHP]$initial->apiTradesCalcProfit($group, $t, $res);[/PHP]

and it throws at me :
Fatal error:  Ice_ObjectPrx::apitradescalcprofit() : expected int value but received null in ice_test.php on line 53

At the same time it perfectly works in python:
res = initial.apiTradesCalcProfit(group, t)

What could cause this error?

Comments

  • mesmes CaliforniaAdministrators, ZeroC Staff Mark SpruiellOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    Hi,

    As described in the manual, "out" parameters must be passed by reference in PHP. Using your example, the final parameter should be changed as follows:
    $initial->apiTradesCalcProfit($group, $t, &$res);
    
    Can you make this change to your program and try it again?

    Cheers,
    Mark
  • xoraxxorax Member Ivan BrykovOrganization: Opticsplanet inc
    mes wrote: »
    Can you make this change to your program and try it again?

    Surely I tried to put ampersand in front of $res. With no success: PHP throws the same Fatal Error..
  • mesmes CaliforniaAdministrators, ZeroC Staff Mark SpruiellOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    xorax wrote: »
    Surely I tried to put ampersand in front of $res. With no success: PHP throws the same Fatal Error..
    If you can post a complete example that demonstrates the problem, we will take a look at it.

    Thanks,
    Mark
  • xoraxxorax Member Ivan BrykovOrganization: Opticsplanet inc
    mes wrote: »
    If you can post a complete example that demonstrates the problem, we will take a look at it.

    No problem. I just thought it would be quite boring:) Here is the snippet of PHP code:
    [PHP]
    Ice_LoadProfile('my_server');
    $base = $ICE->stringToProxy("MT:tcp -h ice.server.tld -p 10000");
    $initial = $base->ice_checkedCast("::MTServer::Morda");
    $symbol = 'EURUSD';
    $group = 'Forex_USD';
    $initial->apiSymbolsGet($symbol, &$sym_info); //works fine!
    $initial->apiHistoryPrices($symbol, &$quote_bid, &$quote_ask, &$quote_time); //works fine either!

    $t = new MTServer_TradeRecord();
    $t->symbol = $symbol;
    $t->volume = 100;
    $t->OpenPrice = $quote_bid;
    $t->ClosePrice = $quote_bid + $sym_info->point;

    $res = new MTServer_TradeRecord();
    $initial->apiTradesCalcProfit($group, $t, &$res); //Fatal Error here :(

    [/PHP]

    And here is some part of my .ice file:
    module MTServer {
    	struct ConSymbol {
    		...a lot of stuff here..
    	};
    	struct TradeRecord {
    		...a lot of stuff here...
    	};
    
    	interface Morda {
    		//return: TRUE or exception
    		int apiSymbolsGet(string symbol, out ConSymbol SymbolStruct) throws ServerError;
    		
    		//return: RET_OK or exception
    		int apiHistoryPrices(string symbol, out double bid, out double ask, out int time) throws ServerError;
    	
    		int apiTradesCalcProfit(string group, TradeRecord TrIn, out TradeRecord TrOut) throws ServerError;
    		
    	};
    
    };
    

    And here is python code that works ok (just to compare):
    import Ice
    Ice.loadSlice('My.ice')
    import MTServer
    
    base = Ice.initialize().stringToProxy(MT:tcp -h ice.server.tld -p 10000')
    mtsrv = MTServer.MordaPrx.checkedCast(base)
    
    symbol = "EURUSD"
    
    sg = mtsrv.apiSymbolsGet(symbol)
    
    prices = mtsrv.apiHistoryPrices(symbol)
    t = MTServer.TradeRecord()
    
    t.symbol = symbol
    t.volume=100
    t.OpenPrice = prices[1]
    t.ClosePrice = prices[1] + sg[1].point
    res = mtsrv.apiTradesCalcProfit("Forex_USD", t)
    
    print res #prints what expected
    
    
  • mesmes CaliforniaAdministrators, ZeroC Staff Mark SpruiellOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    Hi,

    I suspect the problem is caused by an uninitialized integer data member in your structure; PHP initializes the member to a null value by default, but the Ice extension expects an integer value. The Ice extension only accepts a null value for strings, objects and proxies; all other types must be initialized with a suitable default value.

    We'll look at improving this situation in a future release.

    Take care,
    Mark
  • xoraxxorax Member Ivan BrykovOrganization: Opticsplanet inc
    mes wrote: »
    I suspect the problem is caused by an uninitialized integer data member in your structure;

    YES! It solved the problem! Thank you very much. Now after initialization of about 24 members of structure (there are ints and doubles too) it finally started working as expected. You saved my day:)
  • mesmes CaliforniaAdministrators, ZeroC Staff Mark SpruiellOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    Hi,

    I'm glad to hear that you resolved the problem. Note that the final release of Ice 3.3.0 will add support for constructors in structures, exceptions and classes that initialize data members to legal default values.

    Take care,
    Mark
Sign In or Register to comment.