Home > Backend Development > PHP Tutorial > 如何理解变量引用?

如何理解变量引用?

WBOY
Release: 2016-06-06 20:34:22
Original
1529 people have browsed it

<code>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;
}
</code>
Copy after login
Copy after login

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

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

代码来源

回复内容:

<code>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;
}
</code>
Copy after login
Copy after login

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

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

代码来源

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

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

<code>class A
{
    public $foo = 1;
}

$a = new A;

$b = $a;

echo $b->foo; # 1

$a->foo = 2;

echo $b->foo; # 2
</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template