Jump to content

[SOLVED] Objects without classes


scottybwoy

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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();");

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.