dongze8698 2010-08-01 19:19
浏览 13

映射所需属性的对象签名

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 own 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.

  • 写回答

1条回答 默认 最新

  • dongxiangshen7916 2010-08-01 19:54
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)