Php $model之间的差异->;属性和$model->;Yii中的setAttributes()

Php $model之间的差异->;属性和$model->;Yii中的setAttributes(),php,yii,Php,Yii,每当我使用$model->attributes=$\u POST['Users']时,它都会从用户表单保存数据 当我使用$model->setAttributes($\u POST['Users'])时,它还保存用户表单中的数据 因此,有人能澄清这两种代码之间的区别吗?正如Yii维基中所述,您可以使用其中任何一种。使用$model->attributes可以直接设置变量。使用$model->setAttributes()可以通过所谓的“setter方法”设置变量 我将使用setter方法而不是

每当我使用
$model->attributes=$\u POST['Users']
时,它都会从用户表单保存数据

当我使用
$model->setAttributes($\u POST['Users'])
时,它还保存用户表单中的数据


因此,有人能澄清这两种代码之间的区别吗?

正如Yii维基中所述,您可以使用其中任何一种。使用
$model->attributes
可以直接设置变量。使用
$model->setAttributes()
可以通过所谓的“setter方法”设置变量

我将使用setter方法而不是直接调用变量,因为您可以在setter方法中添加一行,它将应用于它的所有调用,这将使您在将来避免很多麻烦

例如:

class Model {
  public $attributes;

  public function setAttributes($attributes) {
    $this->attributes = $attributes;
  }
  public function getAttributes() {
    return $this->attributes;
  }
}

$model = new Model();
$model->setAttributes("Foo");
echo $model->getAttributes();
$model->setAttributes("Bar");
echo $model->getAttributes();
class Model {
  public $attributes;

  public function setAttributes($attributes) {
    $this->attributes = $attributes . "-Bar";
  }
  public function getAttributes() {
    return $this->attributes;
  }
}

$model = new Model();
$model->setAttributes("Foo");
echo $model->getAttributes();
$model->setAttributes("Bar");
echo $model->getAttributes();
因此,现在如果您想对属性进行额外的操作,可以将其添加到
setAttributes()
方法中,而不是更改两行代码,只可以更改一行

例如:

class Model {
  public $attributes;

  public function setAttributes($attributes) {
    $this->attributes = $attributes;
  }
  public function getAttributes() {
    return $this->attributes;
  }
}

$model = new Model();
$model->setAttributes("Foo");
echo $model->getAttributes();
$model->setAttributes("Bar");
echo $model->getAttributes();
class Model {
  public $attributes;

  public function setAttributes($attributes) {
    $this->attributes = $attributes . "-Bar";
  }
  public function getAttributes() {
    return $this->attributes;
  }
}

$model = new Model();
$model->setAttributes("Foo");
echo $model->getAttributes();
$model->setAttributes("Bar");
echo $model->getAttributes();

现在,将其扩展到一个级别,在这个级别上,更改数千行代码而不是更改两个setter方法将很不方便。

这绝对没有区别

当您尝试在上分配一个未定义为PHP类属性的属性(例如这里的
attributes
)时,Yii按惯例调用类似命名的setter方法
setAttributes
。如果不存在这样的方法,将引发异常。由于Yii模型是一个组件,并且模型没有
属性
属性,因此即使使用第一个表单,也会调用setter方法

所有这些都在手册中

$model->attributes=$_POST['Users']// means setting value of property directly while
$model->setAttributes($_POST['Users']) //is method or function which is indirectly set value of $model->attributes property;  
让我们举个例子

  class Model{
        public $attributes;


         public function setAttributes($att){
             $this->attributes=$att;

        }


  }

            //Now the value of $attributes can be set by two way


 $model = new Model();
 $model->attributes=$value; // 1st way
 $model->setAttributes($value); //2nd way

使用
$this->setAttributes()
您可以分配不安全的属性,但不能使用
$this->attributes

分配不安全属性:

$this->setAttributes($_POST['Model'], false);

更多信息请访问:

没有区别。array\u merge用于合并属性,如果稍后设置,我只是猜测,但是
setAttributes()
也应该运行一些验证规则。这个示例完全错误,没有抓住要点。Yii模型继承的代码使其工作方式不同。没有
$attributes
属性。这不是Yii。它的OPP PHP的一般概念和示例不是Yii代码,它是唯一的一般示例,但问题是关于Yii的,这就是为什么这没有抓住重点。是的,但问题与一般的OPP PHP概念有关。任何对Yii不了解但对OPP PHP不了解的人都可以回答。请在你的回答中包含链接的必要部分,因为链接在一定时间内会死掉。我指的是上面提到的示例。@CaffeineCoder:我不觉得它们是必要的。在任何情况下,还有其他答案确实有例子,所以我只是重复。去投票吧!:-)@马科斯:如果你按照问题所说的去做,没有什么区别,这就是我要回答的。如果您没有完全按照问题所说的去做,那么从技术上讲还有另一个区别:设置属性是不区分大小写的,调用方法是区分大小写的。我无意提供手册中已有的所有信息,也无意涵盖对问题的任意更改。我对您的回答@Marcos感到满意。