首页 > 如何理解变量引用?

如何理解变量引用?

public function search($params)
{
    $query = Post::find();

    $dataProvider = new ActiveDataProvider([

        # @1
        'query' => $query,
    ]);

    # @2
    $query->andFilterWhere(['id' => $this->id]);
    $query->andFilterWhere(['like', 'title', $this->title])
          ->andFilterWhere(['like', 'creation_date', $this->creation_date]);

    # @3
    return $dataProvider;
}

在 @1 处, $query 这个变量赋给了 $dataProvider的 query 属性;
在 @2 处, $query 有增加了一些查询条件;

请问, 在 @3 处, $dataProvider 的 query 属性, 怎么会拥有 $query 在 @2 处增加的那些条件?

代码来源


在php5,一个对象变量已经不再保存整个对象的值。只是保存一个标识符来访问真正的对象内容。

class A
{
    public $foo = 1;
}

$a = new A;

$b = $a;

echo $b->foo; # 1

$a->foo = 2;

echo $b->foo; # 2

参看http://php.net/manual/en/language.oop5.references.php

【热门文章】
【热门文章】