Will copy-on-write prevent data duplication on arrays?

前端 未结 2 400
無奈伤痛
無奈伤痛 2020-11-29 11:18

I am programming a web API client in PHP that parses CSV data into associative arrays and I want to protect my users from data-duplication when using these arrays.

M

相关标签:
2条回答
  • 2020-11-29 11:54

    Copy on write as the name suggests means no variable is being copied until something is written; as long as not a single byte is changed in the variable passed around, PHP takes care of avoiding unnecesary duplicates automatically and without the need of using explicit references thanks to this mechanism.

    This article explains in detail how is this implemented in the source code of PHP, and as the article suggests, using xdebug one can easily check the variables are not being duplicated with the function xdebug_debug_zval.

    Additionally this answer here on SO has more on Copy-on-Write.

    0 讨论(0)
  • 2020-11-29 12:11

    If you don't change them, the arrays will not be copied.

    0 讨论(0)
提交回复
热议问题