Note that there are some explanatory texts on larger screens.

plurals
  1. POarray_pop, array_shift etc. on overloaded properties
    text
    copied!<p>I'm in a rut and I'm not sure why I elected for this solution. Something to do with building a simple ORM for MongoDB which has got way more complicated than I imagined. That's not the point though.</p> <p>Anyway, I've got all of the document data stored in a <code>protected $_data</code> variable, and the magic methods <code>__get</code>, <code>__set</code> etc. all map to this array.</p> <p>What I'm after is to modify overloaded variables using some of the following code:</p> <pre> // Dynamically push using $model->comments += array('comment' => 'Oh hi there amigo.'); // Alternatively, to push: array_push($model->comments, array('comment' => 'Oh hi there amigo.')); // Plus, pop, shift, unshift etc. array_{verb}($model->comments); </pre> <p>Basically, PHP is being a pain and throwing a notification saying:</p> <pre><code> Indirect modification of overloaded property Model_Blogpost::$comments has no effect </code></pre> <p>I'm not enjoying this one bit. If this logic could go in __set and __get that would remove the need for me to make separate classes for every datatype mongo takes. At least, that's what my logic is saying now.</p> <p>Does anyone know if something like this is possible? Or do we need to do our own home grown functions to sort this sort of stuff out (as a last resort)?</p> <p>Thanks in advance, your confused friend.</p> <p>Edit: </p> <p>The PHP version is 5.3.6 and returning by reference as in <code>&amp;__get</code> is a bit tricky — here's a quick overview of the code:</p> <p><a href="https://github.com/dynamism/Mundo/blob/develop/classes/mundo/object/core.php" rel="nofollow">https://github.com/dynamism/Mundo/blob/develop/classes/mundo/object/core.php</a></p> <p>Basically, <code>__get</code> calls <code>get()</code> to reduce code duplication. <code>get()</code> merges the loaded model data (<code>$_data</code>) with changed but unsaved model data (<code>$_changed</code>) to give the most recent data type.</p> <p>It does this by flattening the original and changed data into a single dimension array dot using notation and merges the two. Mongo is so loose it's a pain. (FYI: you can use <code>original()</code> and <code>changed()</code> to get only <code>$_data</code> or <code>$_changed</code> respectively).</p> <p>This means that it's not quite as simple as returning part of an array by reference. If that is the only way to do it, though, it looks like it will have to be refactored?</p>
 

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