php中 用法,php中的“->”的用法

//比如

$array1=array('a'=>5,'b'=>6);

while($arrayitem=each($array1)){

extract($arrayitem);

echo('
'.$key.'='.$value);

}

输出:

a=5

b=6

引用一个类的属性和方法就使用->符号。

->是调用的意思

类 -> 类的成员变量或者成员函数

example:

//定义类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

还有一个=>这样的箭头,定义数组用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值