scottybwoy Posted July 29, 2008 Share Posted July 29, 2008 Do you have to create a class to use object structure? I have a json file that I want to write to, but it likes to be fed objects. My php script can make an array, but the syntax is different. Let me give you some code : $modelId = $v['modelId']; $stockTotal = $stockArr['stockTotal']; $file = "models$cat"; $modelId->stock = $stockTotal; $fnames = array($file => $modelId); array_push($models_stockArr, $fnames); So basically I have three bits of data here; $modelId, $stockTotal, and $file. I want the original name of the object to be whatever the $modelId is, which then has a ->stock attribute, with the value of $stockTotal. The $modelId objects then need to go into the $file array and then the $file arrays get pushed into the $models_stockArr. So it should be simple, but the php manual isn't that clear on objects without classes. Can I just do something like $obj = new $modelId; Fatal error, guess you can't, any other ways around it? Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted July 29, 2008 Author Share Posted July 29, 2008 Do you have to create a class to use object structure? I have a json file that I want to write to, but it likes to be fed objects. My php script can make an array, but the syntax is different. Let me give you some code : $modelId = $v['modelId']; $stockTotal = $stockArr['stockTotal']; $file = "models$cat"; $modelId->stock = $stockTotal; $fnames = array($file => $modelId); array_push($models_stockArr, $fnames); So basically I have three bits of data here; $modelId, $stockTotal, and $file. I want the original name of the object to be whatever the $modelId is, which then has a ->stock attribute, with the value of $stockTotal. The $modelId objects then need to go into the $file array and then the $file arrays get pushed into the $models_stockArr. So it should be simple, but the php manual isn't that clear on objects without classes. Can I just do something like $obj = new $modelId; Fatal error, guess you can't, any other ways around it? Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted July 29, 2008 Share Posted July 29, 2008 posting twice isn't going to speed things up. Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted July 29, 2008 Author Share Posted July 29, 2008 Browser Error, I hope one of the moderators can clean the other one up. Sorry for the mess Quote Link to comment Share on other sites More sharing options...
gorgo666 Posted July 29, 2008 Share Posted July 29, 2008 Ummm not that I know of personally. I know that you can refer to properties and methods within a class without having to instantiate the class using the :: operator, I think. And I know that you could use abstraction to create a class with protected fields that cannot be directly instantiated as an object, but rather only extended by child classes... Hope that helps. Quote Link to comment Share on other sites More sharing options...
gorgo666 Posted July 29, 2008 Share Posted July 29, 2008 Err double post, lol, I posted on the other topic. Quote Link to comment Share on other sites More sharing options...
samshel Posted July 29, 2008 Share Posted July 29, 2008 as far as i know you cannot do this..if your function needs an object as an input you will have to create a class...in your case may be a dynamic class... something like this...BEWARE if you are running this code multiple times (say 1,00,000 times) it may cause some performance issues,. $modelId = $v['modelId']; $stockTotal = $stockArr['stockTotal']; $file = "models$cat"; exec("$strTime= 'dynamicclass'.time(); class $strTime{var stock = $stockTotal;} $modelId = new $strTime();"); //$modelId->stock = $stockTotal; $fnames = array($file => $modelId); array_push($models_stockArr, $fnames); Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted July 29, 2008 Author Share Posted July 29, 2008 Thanks samshel, I tried using your code, but it still only gives the $modelId as a value in the $file array. Which is what I had before. It won't be run 1,000,000's of times, only by the amount of products sold in an order, so the maximum is dependant on how many products we have, which at present is ~ 150. Strangely if I change the name to $obj, and dump the result it is empty, whilst $modelId remains the actual modelId, so therefore, it doesn't seem to be doing anything. Do I need to have specific settings to use this exec? Also I wanted the object name to be the same as the modelId so I changed it a bit. My code looks like such now, let me know where I've gone wrong $modelId = $v['modelId']; $stockTotal = $stockArr['stockTotal']; $file = "models$cat"; $class = $modelId; exec("class $class { var stock = $stockTotal; } $obj = new $strTime();"); $fnames = array($file => $obj); array_push($models_stockArr, $fnames); As I said with your code it just returns $modelId as $modelId not an object, with my ammended one, it is blank. Thanks Quote Link to comment Share on other sites More sharing options...
samshel Posted July 29, 2008 Share Posted July 29, 2008 model id will remain as model id as you are not changing it anywhere... I want the original name of the object to be whatever the $modelId is, you want the name of the object as model id OR the name of the CLASS. in your code it is class... try this... xec("$strTime= 'dynamicclass'.time(); class $strTime{var stock = $stockTotal;} ${$modelId} = new $strTime();"); Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted July 29, 2008 Author Share Posted July 29, 2008 Ok, lets say the value of $modelId is "network_card" and the value of $file is "models5" and $stockTotal = 60. I want $models_stockArr to look something like this : $models_stockArr = array("models5" => Obj["network_card" -> stock : 60]) There will be different models for the files and then different files, so I'm trying to build something like : $models_stockArr = array("models5" => (Obj["network_card" -> stock : 60],["24port_switch" -> stock : 10]), "models6" => (Obj["firewire_card" -> stock : 35])) etc. I hope that clears things up a bit. Cheers Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted July 30, 2008 Author Share Posted July 30, 2008 bump... Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted July 31, 2008 Author Share Posted July 31, 2008 Do I need to use a set_var declaration along with output in my exec to use the object? What would I need to put for those parameter? Quote Link to comment Share on other sites More sharing options...
trq Posted July 31, 2008 Share Posted July 31, 2008 Simply using object syntax... <?php $foo->bar = 'bob'; $foo->boo = 'betty'; ?> will create an object ($foo) of the type stdClass. Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted August 1, 2008 Author Share Posted August 1, 2008 I tried that first and I got a fatal error. But I can see how this exec() could work, even though it may be a bit slower. Quote Link to comment Share on other sites More sharing options...
trq Posted August 1, 2008 Share Posted August 1, 2008 What version of php are you using? This... <?php $foo->bar = 'bob'; $foo->boo = 'betty'; print_r($foo); ?> produces.... stdClass Object ( [bar] => bob [boo] => betty ) On php 5.2.6 Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted August 4, 2008 Author Share Posted August 4, 2008 I am using php version 5.2.5. If you look at my first bit of code you can see that I tried that first. All it returns via print_r is the value of $modelId it doesn't convert it to an object. Any other ideas? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 4, 2008 Share Posted August 4, 2008 Do you have to create a class to use object structure? I have a json file that I want to write to, but it likes to be fed objects. My php script can make an array, but the syntax is different. Let me give you some code : $modelId = $v['modelId']; $stockTotal = $stockArr['stockTotal']; $file = "models$cat"; $modelId->stock = $stockTotal; $fnames = array($file => $modelId); array_push($models_stockArr, $fnames); So basically I have three bits of data here; $modelId, $stockTotal, and $file. I want the original name of the object to be whatever the $modelId is, which then has a ->stock attribute, with the value of $stockTotal. The $modelId objects then need to go into the $file array and then the $file arrays get pushed into the $models_stockArr. So it should be simple, but the php manual isn't that clear on objects without classes. Can I just do something like $obj = new $modelId; Fatal error, guess you can't, any other ways around it? You'll need to use the (object) type cast to convert the array to object form: $modelId = (object) $v['modelId']; $modelId->stock = $stockTotal; Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted August 4, 2008 Author Share Posted August 4, 2008 Thanks Wildteen88, I think we are finally getting somewhere. That produced this : stdClass Object ( [scalar] => PCIDS/LP/R [stock] => -20 ) What I really need is (using examples in above post): stdClass Object ( [network_card] => stdClass Object ( [stock] => 60 ) ) I finally got that by using : $modObj = new stdClass(); $modObj->$modelId->stock = $stockTotal; Thanks everyone that helped me get there. Cheers again Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.