Why should I need "&"?
See example on the web about the PHP Factory Pattern.
In the line $kind =& Vehicle::category($wheel);
, why should I to use &
?
The code:
<?php
class Vehicle {
function category($wheel = 0) {
if ($wheel == 2) {
return "Motor";
} elseif($wheel == 4) {
return "Car";
}
}
}
class Spec {
var $wheel = '';
function Spec($wheel) {
$this->wheel = $wheel;
}
function name() {
$wheel = $this->wheel;
return Vehicle::category($wheel);
}
function brand() {
$wheel = $this->wheel;
$kind =& Vehicle::category($wheel);
if ($kind == "Motor") {
return array('Harley','Honda');
} elseif($kind = "Car") {
return array('Nisan','Opel');
}
}
}
$kind = new Spec(2);
echo "Kind: ".$kind->name();
echo "<br>";
echo "Brand: " . implode(",", $kind->brand());
?>
这里没有用,因为这个例子得到了一个常量的引用,但是当你想要“观察”一个可能会改变的值时,引用确实有用,并且你希望你的变量随它改变。
From Stack Overflow question Reference - What does this symbol mean in PHP?:
=&
References
As the (deleted) comment said, the last link should apply to your specific case.
链接地址: http://www.djcxy.com/p/57594.html上一篇: 通过引用分配的意义何在?
下一篇: 为什么我需要“&”?