PHP 中的数据模型

发布于 2024-10-14 22:48:52 字数 513 浏览 8 评论 0原文

我正在 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 技术交流群。

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

发布评论

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

评论(2

月野兔 2024-10-21 22:48:52

您将无法在运行时使用 PHP 轻松地完成此操作。

您可以使用现有的 ORM,例如 Doctrine推进

或者您可以预先生成模型类文件,或使用魔术方法

简单示例:

class something {

    private $_model_data;

    public function __call($name, $value = null) {
        if (strpos($name, 'get') === 0) {
            $attribute = substr($name, 3);
            return $this->_model_data[$attribute];
        } else if (strpos($name, 'set') === 0) {
            $attribute = substr($name, 3);
            $this->_model_data[$attribute] = $value;
        }
    }
}

__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:

class something {

    private $_model_data;

    public function __call($name, $value = null) {
        if (strpos($name, 'get') === 0) {
            $attribute = substr($name, 3);
            return $this->_model_data[$attribute];
        } else if (strpos($name, 'set') === 0) {
            $attribute = substr($name, 3);
            $this->_model_data[$attribute] = $value;
        }
    }
}

__call is a magic method that gets called when the method does not exists. So, calling getAttributeName on an instance of the class would call __call with the string getAttributeName as first argument.

梦途 2024-10-21 22:48:52

如果您只想从容器对象的上下文中访问 model_data 数组,则应该使用 __get 魔术方法,或实现 ArrayAccess 接口。

__get 是一种简单的方法,因为实现看起来像这样:

function __get($name)
{
  if (array_key_exists($name, $this->model_data))
  {
    return $this->model_data[$name];
  }
  return;
}

要访问数据,您只需调用对象的属性:$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 the ArrayAccess interface.

__get is the easy way out as the implementation would look something like this:

function __get($name)
{
  if (array_key_exists($name, $this->model_data))
  {
    return $this->model_data[$name];
  }
  return;
}

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.

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