当前位置: 首页 > news >正文

php 常用的接口和函数

ArrayAccess

— interface to provide accessing to objects as arrays 提供以数组形式访问对象的接口。

interface synopsis 接口需要实现下面几个方法

interface ArrayAccess {
/* Methods */
public offsetExists(mixed $offset): bool
public offsetGet(mixed $offset): mixed
public offsetSet(mixed $offset, mixed $value): void
public offsetUnset(mixed $offset): void
}

basic usage 基础用法 实现访问数组的方式访问接口

<?php
class Obj implements ArrayAccess {public $container = ["one"   => 1,"two"   => 2,"three" => 3,];public function offsetSet($offset, $value): void {if (is_null($offset)) {$this->container[] = $value;} else {$this->container[$offset] = $value;}}public function offsetExists($offset): bool {return isset($this->container[$offset]);}public function offsetUnset($offset): void {unset($this->container[$offset]);}public function offsetGet($offset): mixed {return isset($this->container[$offset]) ? $this->container[$offset] : null;}
}$obj = new Obj;var_dump(isset($obj["two"]));
var_dump($obj["two"]);
unset($obj["two"]);
var_dump(isset($obj["two"]));
$obj["two"] = "A value";
var_dump($obj["two"]);
$obj[] = 'Append 1';
$obj[] = 'Append 2';
$obj[] = 'Append 3';
print_r($obj);
?>

the above example will output something similar to : 输出结果

bool(true)
int(2)
bool(false)
string(7) "A value"
obj Object
([container:obj:private] => Array([one] => 1[three] => 3[two] => A value[0] => Append 1[1] => Append 2[2] => Append 3))

php官网ArrayAccess

http://www.lryc.cn/news/217500.html

相关文章:

  • 【Flutter】Flutter 动画深入解析(2):掌握 AnimatedBuilder 将动画的逻辑和 UI 代码分离
  • Spring Boot中解决跨域问题(CORS)
  • 基于生成对抗网络的照片上色动态算法设计与实现 - 深度学习 opencv python 计算机竞赛
  • 广州华锐互动:数字孪生可视化制作软件有哪些亮点?
  • 设计模式之工厂模式讲解与案例
  • (免费领源码)php#MySQL软件测试文档管理系统28035-计算机毕业设计项目选题推荐
  • 05.Oracle数据库对象
  • 某国产中间件企业:提升研发安全能力,助力数字化建设安全发展
  • Servlet中主要的内置对象
  • STL-set和map
  • 【WinForm详细教程四】WinForm中的ProgressBar 、ImageList和ListView控件
  • 写一个简单实用的Excel工具类
  • C#中LINQtoObjects、LINQtoDataSet和LINQtoXML
  • k8s中 RBAC中,clusterrole,serviceaccount , rolebinding 是什么关系谁先谁后
  • 什么是文件安全
  • maven的settings.xml和pom.xml配置文件详解
  • YB2503HV 100V 3A SOP8内置MOS 高效率降压IC(昱灿)
  • Redis安装Linux
  • PCL点云处理(007)-Ransac
  • 有方N58 HTTP POST 请求连接 TDengine
  • 基于Python+Pygame实现一个滑雪小游戏
  • 【限制输入框值类型】自定义指令el-input输入类型限制,vue和html两个版本
  • 对一个金融风控测额公式的理解(1)
  • 【GEE】2、探索数据集
  • 开发一款直播弹幕游戏需要多少钱?
  • STM32F103C8T6第一天:认识STM32 标准库与HAL库 GPIO口 推挽输出与开漏输出
  • selenium元素定位 —— 提高篇 CSS定位元素
  • 隔离和非隔离电源的区别
  • C语言自定义数据类型
  • SoftwareTest5 - 你就只知道功能测试吗 ?