php单箭头和双箭头区别:
引用一个类的属性和方法就使用->符号。
下面是一个例子小程序:<?php
//定义类Cart
class Cart {
var $items; // 购物车中的物品
// 将 $num 个 $artnr 物品加入购物车
function add_item($artnr, $num) {
$this->items[$artnr] += $num;
}
// 将 $num 个 $artnr 物品从购物车中取出
function remove_item($artnr, $num) {
if ($this->items[$artnr] > $num) {
$this->items[$artnr] -= $num;
return true;
} elseif ($this->items[$artnr] == $num) {
unset($this->items[$artnr]);
return true;
} else {
return false;
}
}
}
//示例继承定义类Named_Cart
class Named_Cart extends Cart {
var $owner;
function set_owner ($name) {
$this->owner = $name;
}
}
//使用类的代码
$ncart = new Named_Cart; // 新建一个有名字的购物车
$ncart->set_owner("kris"); // 给该购物车命名
print $ncart->owner; // 输出该购物车主人的名字
$ncart->add_item("10", 1); // (从购物车类中继承来的功能)
?>
“->”这个箭头也可以是调用类中的函数class a { function b() { echo 'a'; } } $a=new a; $a->b(); 输出:a
=>这样的箭头,定义数组用:$array1 = array('a' = >5, 'b' = >6);
while ($arrayitem = each($array1)) {
extract($arrayitem);
echo('
'.$key.'='.$value);
}
输出:a = 5 b = 6
总结:php单箭头“->”用来引用一个类的属性和方法或调用类中的函数。双箭头“=>”用来定义数组。