为什么我需要“&”?
查看网上有关PHP工厂模式的示例。
在行$kind =& Vehicle::category($wheel);
,为什么我要使用&
?
代码:
<?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());
?>
这里没有用,因为这个例子得到了一个常量的引用,但是当你想要“观察”一个可能会改变的值时,引用确实有用,并且你希望你的变量随它改变。
从堆栈溢出问题参考 - 这个符号在PHP中的含义是什么?
=&
参考
正如(删除)评论所说,最后一个链接应该适用于您的具体情况。
链接地址: http://www.djcxy.com/p/57593.html下一篇: What Does the & (ampersand) symbol mean in this circumstance?