PHP 中的数据模型
我正在 php 中尝试非常晦涩的数据模型。例如,如果我在 SQL 数据库中有一个名为“model_attributes”的表。这是我正在处理的模型的所有可用属性的列表。现在,我希望模型和其他模型能够从该列表派生函数,以便在数据从构造函数加载到数组中后从数组中提取数据。这是一个伪代码示例:
for each attributes as attribute
create new function ( getAttributeName )
首先
getAttributeName()
,
return $this->_model_data['attribute_name']
这是一个好的做法吗?看起来很有条理,但同时又显得有点脏。也许有人可以给我一些关于如何在 php 中处理大数据模型的其他建议。
另外,如果有人做过这样的事情,我该如何在 phpDocs 中处理这个问题?
I'm sort of experimenting with very obscure data models in php. For example, if I have a table called "model_attributes" in an SQL database. This is a list of all the available attributes for the model I'm working on. Now, I want the model, and other models to be able to derive functions from this list to pull data out of an array, after the data has been loaded into the array from the constructor. Here is an example in pseudo-code:
for each attributes as attribute
create new function ( getAttributeName )
and
getAttributeName()
would
return $this->_model_data['attribute_name']
First off, is this good practice? It seems organized, but at the same time seems kind of dirty. Maybe someone could give me some other suggestions as to how to handle large data models in php.
Also, if anyone has done something like this, how could I handle this in phpDocs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将无法在运行时使用 PHP 轻松地完成此操作。
您可以使用现有的 ORM,例如 Doctrine 或 推进。
或者您可以预先生成模型类文件,或使用魔术方法。
简单示例:
__call
是一个魔术方法,当该方法不存在时会调用该方法。因此,在类的实例上调用getAttributeName
将会调用__call
,并将字符串getAttributeName
作为第一个参数。You will not be able to do this easily at runtime with PHP.
You may use an existing ORM, for example Doctrine or Propel.
Or you could pre-generate the model class files, or use magic methods.
Quick example:
__call
is a magic method that gets called when the method does not exists. So, callinggetAttributeName
on an instance of the class would call__call
with the stringgetAttributeName
as first argument.如果您只想从容器对象的上下文中访问 model_data 数组,则应该使用
__get
魔术方法,或实现ArrayAccess
接口。__get
是一种简单的方法,因为实现看起来像这样:要访问数据,您只需调用对象的属性:
$obj->property_name
,或者在您使用字符串的情况下:$obj->{$property}
当然,这可能会干扰您拥有的任何公共属性(通常无论如何拥有它们都是一个坏主意)。
或者,您可以将对象设置为 ArrayAccess 的实现,这将允许您通过以下方式访问属性:
$obj['property_name']
或$obj[$property]
如果您想混淆字段名称或调整稍后检索数据的方式,这两种解决方案都将允许您进行自定义访问管理
AFAIK ArrayAccess 方法更快,但您必须对其进行测试才能确定。魔法方法往往会有点慢。
If you just want to access the array of
model_data
from the context of a container object, you should use either the__get
magic method, or implement theArrayAccess
interface.__get
is the easy way out as the implementation would look something like this:To access the data you'd just call the property of the object:
$obj->property_name
, or in the case where you're using a string:$obj->{$property}
Of course, this may interfere with any public attributes you have (generally a bad idea to have them anyway).
Alternatively you can set up your object as an implementation of ArrayAccess, which will allow you to access the property as:
$obj['property_name']
, or$obj[$property]
Both solutions will allow you to have custom access management, if you want to obfuscate your field names, or adjust how the data is retrieved later.
AFAIK the ArrayAccess method is faster, but you'd have to test it to be certain. Magic methods have a tendency to be a bit slow.