重载属性上的 array_pop、array_shift 等
我墨守成规,不确定为什么选择这个解决方案。与为 MongoDB 构建一个简单的 ORM 有关,它比我想象的要复杂得多。但这不是重点。
无论如何,我已经将所有文档数据存储在受保护的 $_data 变量中,以及魔术方法 __get 、 __set 等。映射到这个数组。
我所追求的是使用以下一些代码修改重载变量:
// 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);
基本上,PHP 很痛苦并抛出一个通知说:
Indirect modification of overloaded property Model_Blogpost::$comments has no effect
我一点也不喜欢这个。如果这个逻辑可以放入 __set 和 __get 中,那么我就不需要为 mongo 所采用的每种数据类型创建单独的类。至少,我现在的逻辑是这么说的。
有谁知道这样的事情是否可能?或者我们是否需要自己开发函数来解决此类问题(作为最后的手段)?
预先感谢您困惑的朋友。
编辑:
PHP 版本是 5.3.6,通过引用返回 &__get
有点棘手 - 这是代码的快速概述:
https://github.com/dynamism/Mundo/blob/develop/classes/mundo/object/core.php
基本上, __get
调用 get ()
以减少代码重复。 get()
将加载的模型数据 ($_data
) 与已更改但未保存的模型数据 ($_changed
) 合并,以提供最新的数据类型。
它通过使用符号将原始数据和更改后的数据展平为一维数组点并将两者合并来实现此目的。 Mongo 太松了,很痛苦。 (仅供参考:您可以使用 original()
和 changed()
分别获取 $_data
或 $_changed
)。
这意味着它并不像通过引用返回数组的一部分那么简单。如果这是唯一的方法,那么看起来它必须重构?
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.
Anyway, I've got all of the document data stored in a protected $_data
variable, and the magic methods __get
, __set
etc. all map to this array.
What I'm after is to modify overloaded variables using some of the following code:
// 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);
Basically, PHP is being a pain and throwing a notification saying:
Indirect modification of overloaded property Model_Blogpost::$comments has no effect
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.
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)?
Thanks in advance, your confused friend.
Edit:
The PHP version is 5.3.6 and returning by reference as in &__get
is a bit tricky — here's a quick overview of the code:
https://github.com/dynamism/Mundo/blob/develop/classes/mundo/object/core.php
Basically, __get
calls get()
to reduce code duplication. get()
merges the loaded model data ($_data
) with changed but unsaved model data ($_changed
) to give the most recent data type.
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 original()
and changed()
to get only $_data
or $_changed
respectively).
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要升级您的 PHP。这在后来的版本中得到了修复(不确定是哪个)。在 5.2.9 中为我工作。
如果您使用的是更高版本并且仍然发出通知,您需要确保使用以下签名定义
__get
:public function &__get($val)
You may need to upgrade your PHP. This was fixed in later versions (not sure which). Works for me in 5.2.9.
If you are using a later version and it still issues notices, you'll want to ensure that you are defining
__get
with the signature:public function &__get($val)
Kohana 和 Mongo_Document 类以及 php 5.3.6 遇到了同样的问题,事实证明它想使用自己的方法来添加值,所以这
是一种方法
Got the same problem with Kohana and Mongo_Document class and php 5.3.6, turns out it wants to use own methods for adding values, so
is a way to go