なぜphpの文字列が値型ですか?引数が関数に渡されるたびに、場所にコピーされます。割り当てが行われるたびに、すべての連結によって文字列がコピーされます。私の.NETの経験は、それが非効率的であると私に言い、ほとんどどこでも参照を使用するように強制します。次の選択肢を検討してください。PHP文字列は値型ですか?
オルタナティブ1
// This implementation hurts performance
class X {
public $str;
function __construct($str) { // string copied during argument pass
$this->$str = $str; // string copied here during assignment
}
}
オルタナティブ2
// This implementation hurts security
class Y {
public $str;
function __construct(&$str) {
$this->$str = &$str;
}
}
// because
$var = 'var';
$y = new Y($var);
$var[0] = 'Y';
echo $y->var; // shows 'Yar'
第三の選択
// This implementation is a potential solution, the callee decides
// whether to pass the argument by reference or by value, but
// unfortunately it is considered 'deprecated'
class Z {
public $str;
function __construct($str) {
$this->$str = &$str;
}
}
// but
$var = 'var';
$z = new Z(&$var); // warning jumps out here
$var[0] = 'Z';
echo $y->var; // shows 'Zar'
質問:私はパフォーマンスを選択する必要がありますどのような痛み/セキュリティ/廃止
真剣に?パフォーマンスが痛いですか?リファレンスをどこからでも使用すると、PHPでさらにパフォーマンスが低下することがあります。 – BoltClock
100万個のオブジェクトを初期化したり、引数に膨大な量のデータを渡したりしない限り、パフォーマンスはここでは問題になりません。あなたは何をしているのか現実世界の例を見せてもらえますか? –
さて、すべての画像を1つのファイルに入れ、それを圧縮してdinamicallyにし、CSSのスプライトや、以前はマイクロパフォーマンスではないものを使用していたとしますか?また、これがパフォーマンス上の問題となる実際のケースがある場合、C++のような本当の強力な言語を使用しない理由を理解できません。 – Dave