PHP 中的 SplObjectStorage 和糖语法
快一个;我怀疑这是可能的,但是有什么方法可以利用 PHP 的 array($key => $value);
语法来处理 SplObjectStorage
对象吗?
我的意思是,有没有这样的方法来实现:
$store = // ?
new KeyObject() => new ValueObject(),
new KeyObject() => new ValueObject(),
// ...
在上下文中初始化对象存储?目前我只是使用:(并且可能会继续,考虑到这种可能性完全不可能)
$store = new SplObjectStorage();
$store[new KeyObject()] = new ValueObject();
$store[new KeyObject()] = new ValueObject();
// ...
会很好,高度怀疑它,但也许有人知道得更好。
Quick one; I doubt it's possible, but is there any way to take advantage of the array($key => $value);
syntax of PHP with regard to SplObjectStorage
objects?
What I mean is, is there any such way to achieve:
$store = // ?
new KeyObject() => new ValueObject(),
new KeyObject() => new ValueObject(),
// ...
In the context initializing an object store? As of the moment I'm simply using: (and will probably continue, considering the sheer unlikeliness of this being a possibility)
$store = new SplObjectStorage();
$store[new KeyObject()] = new ValueObject();
$store[new KeyObject()] = new ValueObject();
// ...
Would be nice, highly doubting it, but maybe someone knows better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然这将是一种更简洁的语法,但不幸的是这是不可能的。您可以做的最好的事情是:
或者
将对象添加到
SplObjectStorage
时。While it would be a more concise syntax, unfortunately it's not possible. The best you can do is either:
or
When adding object to an
SplObjectStorage
.为什么不做这样的事情:
它不漂亮,但至少简洁。
Why not do something like that:
It's not beautiful but it's at least concise.