次のコードの違いは何ですか?
$this->idKey;
(この場合example
)$this
オブジェクトの変数プロパティ名を読み取りので$this->example
:として
$idKey = 'example';
$this->$idKey;
同じ
$this->idKey
$this->$idKey
$this->{$idKey}
次のコードの違いは何ですか?
$this->idKey;
(この場合example
)$this
オブジェクトの変数プロパティ名を読み取りので$this->example
:として
$idKey = 'example';
$this->$idKey;
同じ
$this->idKey
$this->$idKey
$this->{$idKey}
は$this
オブジェクトのidkey
プロパティを読み取り上記($this->example
)ですが、あいまい性はほとんどありません(オペランドを制御するために括弧を追加するのと同様です)いくつかのケースでRDER、かつ有用):
$idKey = 'example';
$this->{$idKey};
ます$ this-> IDKEYこれはあなたがPHP
Class Car{ //member properties var $color; function printColor(){ echo $this->color; //accessing the member property color. } }
でオブジェクトのプロパティにアクセスする方法です$ this - > $ idKey
プロパティ名自体を可変
$attribute ='color'
$this->$attribute // is equivalent to $this->color
に格納されているときは、これは$を使用することができる - > { '$ IDKEY'}
は、明示的な形でありますクラスのプロパティにアクセスするもう1つの目的を果たします。 a valid variable nameです。
$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
echo $o->123foo; // error
だから、この
$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
echo $o->{'123foo'}; // OK!
$this->idKey
を解決するために表現中括弧を使用することができスコープオブジェクトのプロパティidKey
です。
$this->$idKey
と$this->{$idKey}
は、$idKey
という名前のプロパティにアクセスしている場合と同じ結果になります。
class ButtHaver{
public idKey;
public buttSize;
}
$b = new ButtHaver();
$b->idKey = 'buttSize';
$b->buttSize = 'Large';
echo $b->idKey; // outputs 'buttSize'
echo $b->$idKey; // outputs 'Large'
echo $b->{$idKey}; // outputs 'Large'
${$}
構文は、あなたが望む可変であるアップクリアする$$a[1]
のような特定のケースであいまいさを解決することです。変数$ aに指定された配列の配列の値で指定された変数の場合は${$a[1]}
、変数$ aの名前の配列の場合は${$a}[1]
です。
ここですべて読むことができます:http://php.net/manual/en/language.variables.variable.php