0

私はオブジェクト指向 PHP に関する本を読んでいて、著者が複雑な構文を使用している場合があることに気付きました。継承に関する章で、彼は以下のコードを使用しています。

// Declare the getSummaryLine() method
function getSummaryLine() {
// Define what the getSummaryLine() method does
     $base  = "$this->title ( {$this->producerMainName}, ";
     $base .= "{$this->producerFirstName} )";
     return $base;
}

私の質問は、なぜあなたはただ使わないのですか:

// Declare the getSummaryLine() method
function getSummaryLine() {
// Define what the getSummaryLine() method does
     return "$this->title ( $this->producerMainName, $this->producerFirstName )";
}

どちらも同じものを返すように見えますか?

これが痛いほど明白である場合は、ご容赦ください。マニュアルで PHP の複雑な構文を読みましたが、これ以上明確にはなりませんでした。セキュリティの問題なのか、スタイルの選択なのか、それともまったく別のものなのか?

4

3 に答える 3

2

この場合、それはスタイル/好みの問題です

複数の行にまたがり、変数が中かっこで囲まれていると、読みやすいと感じるかもしれません。

于 2012-11-25T18:20:24.327 に答える
2

どちらも同じことを達成しますが、複合ステートメントの理由は読みやすさに関係しています。より長い連結された文字列は単純に読みやすく、作成者側のコード フレーバーにすぎません。

これに関する複雑な部分は、評価に関係しています。中括弧を使用すると、次のことができます。

echo "This works: {$arr['key']}";
于 2012-11-25T18:20:02.813 に答える
1

これらはすべて有効です。

著者は、読みやすくするためだけに連結を使用した可能性があります。長いコード行は本でもうまくいきません。

二重引用符内に配置する場合、配列/オブジェクトからの文字列を {} で囲む必要がある場合があります。そうしないと、構文エラーが表示されます。

// Declare the getSummaryLine() method
function getSummaryLine() {
// Define what the getSummaryLine() method does
     $base  = "$this->title ( {$this->producerMainName}, ";
     $base .= "{$this->producerFirstName} )";
     return $base;
}

また

// Declare the getSummaryLine() method
function getSummaryLine() {
// Define what the getSummaryLine() method does
     return "{$this->title} ( {$this->producerMainName}, {$this->producerFirstName} )";
}

または

// Declare the getSummaryLine() method
function getSummaryLine() {
// Define what the getSummaryLine() method does
     return $this->title.'( '.$this->producerMainName.', '.$this->producerFirstName.' )';
}
于 2012-11-25T18:27:06.420 に答える