重载属性上的 array_pop、array_shift 等

发布于 2024-12-02 15:52:24 字数 1446 浏览 0 评论 0原文

我墨守成规,不确定为什么选择这个解决方案。与为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清浅ˋ旧时光 2024-12-09 15:52:24

您可能需要升级您的 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)

酒与心事 2024-12-09 15:52:24

Kohana 和 Mongo_Document 类以及 php 5.3.6 遇到了同样的问题,事实证明它想使用自己的方法来添加值,所以这

$model->push('comments',array('comment' => 'Oh hi there amigo.'));

是一种方法

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

$model->push('comments',array('comment' => 'Oh hi there amigo.'));

is a way to go

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文