怎么在php中使用this关键字

怎么在php中使用this关键字

今天就跟大家聊聊有关怎么在php中使用this关键字,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

下面定义了一个Cart类

<?php
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;
    } else {
      return false;
    }
  }
}
?>

看完上述内容,你们对怎么在php中使用this关键字有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。