在数组类属性上使用 __get() 来自动传播类
我有一个 activerecord 模型,其中有一个数组:“$sessions”。我想做的是将其设为二维数组,以便按以下方式进行索引:
["0"=>Session(Object), "1"=>Session(Object)]
现在要在顶部添加奶油,我想将其制作为用户可以执行的操作:
user->sessions[1]->id = "ghgh"
以及 __get
将了解类变量(“$sessions”)中的该位置是否已经存在一个对象,并向该类添加/编辑该属性,否则它将在该位置创建一个名为“Session”的新类。
我有点困惑如何让 PHP(如果可能的话)在数组的索引上获取 __get 。
I have a activerecord model which has an array in it of say: "$sessions". What I would like to do is make this a 2d array so as to be indexed in the following way:
["0"=>Session(Object), "1"=>Session(Object)]
Now to add cream on top I would like to make it so the user could do:
user->sessions[1]->id = "ghgh"
And the __get
would understand whether there is an object already in that position in the class variable ("$sessions") and add/edit the property to the class or it will make a new class in that position called "Session".
I am a bit confused how I could get PHP (if possible) to get a __get on an index of an array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正在寻找 ArrayAccess。
I think you're looking for ArrayAccess.