how to concat a counter for an object declaration in PHP -


i trying create multiple instances of object how wrapper designed work. problem wanted add counter in object declaration have loop through data , create necessary object , loop again wrapper read them all.

currently, have this:

if(sizeof($product_name) > 0){                 for($counter=0;$counter<sizeof($product_name);$counter++){                     $lineitem.$counter = new lineitem($this->_xi);                     $lineitem.$counter->setaccountcode('200')                         ->setquantity($product_qty[$counter])                         ->setdescription($product_name[$counter])                         ->setunitamount($product_price[$counter]);                     print_r($lineitem.$counter);                 }             }  print_r($lineitem0); 

my print_r returns nothing both inside , outside loop.

your problem not oop, more php. want create dynamic variable name store instances of class creating, should do:

if(sizeof($product_name) > 0){   for($counter=0;$counter<sizeof($product_name);$counter++){       ${"$lineitem$counter"} = new lineitem($this->_xi);       ${"$lineitem$counter"}->setaccountcode('200')         ->setquantity($product_qty[$counter])         ->setdescription($product_name[$counter])         ->setunitamount($product_price[$counter]);                 print_r(${"$lineitem$counter"});   } }  print_r(${"$lineitem" . 0}); 

have @ question: dynamic variable names in php


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -