从发布的数据创建对象实例-PHP [英] Creating Object instance from Posted data - PHP

查看:68
本文介绍了从发布的数据创建对象实例-PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从表单传递的值中创建和填充对象的最佳方法是什么?

What is the best way of creating and populating an Object from values passed in from a form?

例如,如果我有一个具有颜色,型号,品牌,年份和属性保存"的汽车类,它将插入或更新记录.

If for example, i have a class Car with properties Colour, Model, Make, Year and a method Save, which will either insert or update the record.

然后我有一个表单,其中包含所有这些值的字段,并且已提交.我想用发布的值创建Car类的实例,然后调用Save方法.一切都很好.

I then have a form that has fields for all these values and it is submitted. I want to create an instance of class Car with the posted values and then call the Save method. All good and well.

但是将发布的值分配给对象内部属性的最佳方法是什么.假设这是一个简化的场景,并且实际情况将具有更多的属性,那么单个Set调用就会陷入困境.

But what is the best way of assigning the posted values to the objects internal properties. Assuming this is a simplified scenario and the actual situation would have many more properties, making individual Set calls long-winded.

最好为每个方法简单地调用Set方法吗?还是将数组传递给方法(或构造函数),然后调用Set方法?还是其他方式?

Is it best to simply call the Set method for each one? Or pass in an array to a method (or the constructor) which then calls the Set methods? Or some other way?

任何关于最佳做法的建议都将受到赞赏

Any advice on best practices is appreciated

欢呼 斯图尔特

推荐答案

我将实现魔术功能

I would implement the magic function __set_state(), as it is intended for exactly this use case. There are multiple benefits of putting the implementation into that method:

  1. 此函数的定义非常明确(与您自己的源代码相反,它在PHP文档中定义)
  2. 您可以访问该功能内的私人成员
  3. 使用 var_export() 转储的对象将使用此功能.

编辑,根据评论的要求:

class A {
    private $member = 1000;
    public static function test(A $a) {
        echo $a->member;
    }
}
echo A::test(new A()); // outputs 1000

编辑:从注释中满足另一个请求:

EDIT Fulfilling another request from the comments:

除非您使用的是php 5.3,否则您不知道在哪个类上调用了静态方法,其中所需要的功能( ).如果您具有有效的 get_called_class() 函数,则可以请执行以下操作:

You cannot know on which class a static method was called unless you are using php 5.3, in which the required feature (late static binding) was introduced. What you can do in emulating get_called_class() by analyzing the current stack (using debug_backtrace()). If you have a working get_called_class() function, you can do the following:

class BaseClass {
    public static function __set_state($state) {
        $class = get_called_class();
        // Assumption: Constructor can be invoked without parameters!
        $object = new $class();
        foreach (get_class_vars($class) as $member) {
            if (!isset($state[$member])) {
                // Member was not provided, you can choose to ignore this case
                throw new Exception("$class::$member was not provided");
            } else {
                // Set member directly
                // Assumption: members are all public or protected
                $object->$member = $state[$member];
                // Or use the setter-approach given by Chaos.
                $setter = 'set' . ucfirst($member);
                $object->setter($state[$member]);
            }
        }
        return $object;
    }
}

这篇关于从发布的数据创建对象实例-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆