Note that there are some explanatory texts on larger screens.

plurals
  1. POare php references a good idea when handling large or many arrays?
    primarykey
    data
    text
    <p>Finally I have something to ask the community</p> <p>My case: I have some box-like objects with internal arrays, I wish to manipulate these arrays inside methods, but a donating box must result empty after a merging operation. I'm currently using references so as to not copy large arrays, but I just recently came across a great post: <a href="http://schlueters.de/blog/archives/125-Do-not-use-PHP-references.html" rel="nofollow" title="post">do not use php references</a>.</p> <pre><code>class Box implements MadeUpArrayLikeInterfaces { protected $v = array(); ... public function __clone() { foreach ($this-&gt;v as &amp;$v) {// correct the mess left behind the cloning process? if (is_object($v)) $v = clone $v;// perhaps I shouldn't deep clone anything? } } public function merge() { $boxes = func_get_args();// variable argument list arary_unshift($boxes,$this-&gt;v); array_walk(array($this,'walkUnwrap'),$boxes);// prepare input, allow only arrays $this-&gt;v = call_user_func('array_merge',array_filter($boxes));// merge } public function import(self $box) {// edited to make more sense $this-&gt;v = array_merge($this-&gt;v,$box-&gt;v);// do the merge $box-&gt;v = array(); } public function export() { $e = array(); // empty array that may be referenced $r =&amp; $this-&gt;v; // avoid copying for return $this-&gt;v =&amp; $e; // point to empty because assignment would erase $r as well return $r; // return contents } private static function walkUnwrap(&amp;$v) {// walks on argument list if ($v instanceof self &amp;&amp; $v-&gt;v) {// if element is Box, export it $e = array(); $o = $v; // save reference to the Box $v =&amp; $v-&gt;v;// pass the data $o-&gt;v =&amp; $e;// this box should now be empty } elseif (!is_array($v)) $v = null;// if it's anything but array get rid of it } } </code></pre> <p>So now I'm wondering if my approach is actually correct. Are these operations "writes" as stipulated in the post and therefore it's better to use references? are these copy-values?</p> <p>Thank you all</p> <p>edited: indeed I have been careless, the import function is much simpler, and I have a better function that also use references that could be useful in showing what this is about</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload