av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

php語言

PHP的ArrayAccess接口

時(shí)間:2025-05-23 02:58:49 php語言 我要投稿
  • 相關(guān)推薦

PHP的ArrayAccess接口

  如果想讓對象使用起來像一個(gè) PHP 數(shù)組,那么我們需要實(shí)現(xiàn) ArrayAccess 接口,就跟隨百分網(wǎng)小編一起去了解下吧,想了解更多相關(guān)信息請持續(xù)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!

  代碼如下:

  interface ArrayAccess

  boolean offsetExists($index)

  mixed offsetGet($index)

  void offsetSet($index, $newvalue)

  void offsetUnset($index)

  下面的例子展示了如何使用這個(gè)接口,例子并不是完整的,但是足夠看懂,:->

  復(fù)制代碼 代碼如下:

  <?php

  class UserToSocialSecurity implements ArrayAccess

  {

  private $db;//一個(gè)包含著數(shù)據(jù)庫訪問方法的對象

  function offsetExists($name)

  {

  return $this->db->userExists($name);

  }

  function offsetGet($name)

  {

  return $this->db->getUserId($name);

  }

  function offsetSet($name, $id)

  {

  $this->db->setUserId($name, $id);

  }

  function offsetUnset($name)

  {

  $this->db->removeUser($name);

  }

  }

  $userMap = new UserToSocialSecurity();

  print "John's ID number is " . $userMap['John'];

  ?>

  實(shí)際上,當(dāng) $userMap['John'] 查找被執(zhí)行時(shí),PHP 調(diào)用了 offsetGet() 方法,由這個(gè)方法再來調(diào)用數(shù)據(jù)庫相關(guān)的 getUserId() 方法。

【PHP的ArrayAccess接口】相關(guān)文章:

php支付寶接口用法分析04-07

php面向?qū)ο笕ヂ?php5接口技術(shù)06-18

PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之接口用法07-08

php根據(jù)IP地址獲取當(dāng)前地理位置接口05-18

Java接口介紹05-08

php學(xué)習(xí)之php配置07-15

PHP學(xué)習(xí):PHP拼音類01-25

php正則去掉php注釋07-30

php與php MySQL之間的關(guān)系03-03