亚欧洲精品在线观看,窝窝影院午夜看片,久久国产成人午夜av影院宅,午夜91,免费国产人成网站,ts在线视频,欧美激情在线一区

php語言

PHP的ArrayAccess接口

時間:2025-05-23 02:58:49 php語言 我要投稿
  • 相關推薦

PHP的ArrayAccess接口

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

  代碼如下:

  interface ArrayAccess

  boolean offsetExists($index)

  mixed offsetGet($index)

  void offsetSet($index, $newvalue)

  void offsetUnset($index)

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

  復制代碼 代碼如下:

  <?php

  class UserToSocialSecurity implements ArrayAccess

  {

  private $db;//一個包含著數(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'];

  ?>

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

【PHP的ArrayAccess接口】相關文章:

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

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

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

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

Java接口介紹05-08

php學習之php配置07-15

PHP學習:PHP拼音類01-25

php正則去掉php注釋07-30

php與php MySQL之間的關系03-03