开发者

Mapping object signatures for required attributes

i am trying to think of the best way to create a way to store the signatures of objects, in particular products. There will be several types of products that will each have their o开发者_如何学JAVAwn set of attributes and therefore different required attributes.

There will be different types of actions performed on the products, i.e validation etc.

At the moment the best i can think of is an associative array which has the attribute, whether its required or not and its data type but surely there must be a better way to do this.


The best I can think of for this situation is an associate array, stored as JSON.

As I don't know where your data is coming from, I'm just going to pretend.

$item['price'] = 123;
$item['otherinfo'] = 'stuff';

$data[] = $item;

// repeat for all data. foreach loop over it if you can

Then, depending how you want to store the data (rewrite every time, append to the file, etc.. Info at w3schools.com for different ways to open a file):

$json = json_encode( $data );
$file = fopen( 'storage.txt', 'w+' ); // opens and clears the file. probably not the best, see link above
fwrite( $file, $json );
fclose( $file );

Then, when you want to load the data:

$string = file_get_contents( 'storage.txt' );
$data = json_decode( $string );

Hope that gets you off to a good start.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜