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

Dynamic Properties in PHP and StdClass

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Languages like JavaScript and Python allow object instances to have dynamic properties. As it turns out, PHP does too. Looking at the official PHP documentation on objects and classes you might be lead to believe dynamic instance properties require custom __get and __set magic methods. They don't.

Simple, Built-in Dynamic Properties

Check out the following code listing:

class DynamicProperties { }
$object = new DynamicProperties;
print isset($object->foo) ? 't' : 'f'; // f

// Set Dynamic Properties foo and fooz
$object->foo = 'bar';
$object->fooz = 'baz';

// Isset and Unset work
isset($object->foo); // true
unset($object->foo);

// Iterate through Properties and Values
foreach($object as $property => $value)  { 
     print($property . ' = ' . $value . '<br />'); 
}

// Prints:
//   fooz = baz

Using the built-in dynamic instance properties is an order of magnitude faster (30x, by my profiling) than using magic __get and __set methods. Built in dynamic property accesses happen without invoking a method call back to PHP script.

So when does it make sense to use __get and __set? If you need more complex behavior, like calculated properties, you must use __get and __set. Also, as an astute comment points out, if you would prefer not to have dynamic properties on a class you can throw errors from __get and __set.

StdClass: Anonymous Objects

Sometimes all that is necessary is a property bag to throw key value pairs into. One way is to use array, but this requires quoting all keys. Another way is to use dynamic properties on an instance of StdClass. StdClass is a sparsely documented class in PHP which has no predefined members.

$object = new StdClass;
$object->foo = 'bar';
json_encode($object);

Next I'll touch on the SPL's Countable and ArrayAccess as a means of being able to accomplish the following in PHP:

class MyClass implements Countable, ArrayAccess { ... }
$myObject = new MyClass();
// Using array access notation
$myObject[0] = 'hello';
$myObject[1] = 'world';
$myObject['foo'] = 'bar';

Thanks to the folks pointing out that you don't need to extend from StdClass in order to have dynamic properties!

转载于:https://my.oschina.net/mickelfeng/blog/138315

相关文章:

  • NoSQL实现(1)——Redis
  • MySQL学习四部曲
  • vim常用命令总结 (转)【转】
  • 深入理解dp px density
  • java基础---一致性hash算法
  • JAVA NIO 选择器
  • Java 集合中常见 checkForComodification()方法的作用? modCount和expectedModCount作用?
  • 旋转数组的最小数字
  • MFC 设置CListCtrl的行高
  • MySQL-Xtrabackup备份还原
  • TF-IDF基本原理
  • 前端日拱一卒D11——ES6笔记之异步篇
  • 关于window对象
  • 深度探索区块链/超级账本系统架构(3)
  • redmine的本地升级与异地迁移升级
  • 【347天】每日项目总结系列085(2018.01.18)
  • Angular 响应式表单 基础例子
  • Angular2开发踩坑系列-生产环境编译
  • Git同步原始仓库到Fork仓库中
  • HTTP--网络协议分层,http历史(二)
  • JDK 6和JDK 7中的substring()方法
  • Mysql优化
  • Terraform入门 - 3. 变更基础设施
  • yii2权限控制rbac之rule详细讲解
  • 构建工具 - 收藏集 - 掘金
  • 力扣(LeetCode)21
  • 前端js -- this指向总结。
  • 全栈开发——Linux
  • 扫描识别控件Dynamic Web TWAIN v12.2发布,改进SSL证书
  • 微信小程序:实现悬浮返回和分享按钮
  • 我从编程教室毕业
  • 携程小程序初体验
  • 再次简单明了总结flex布局,一看就懂...
  • 【云吞铺子】性能抖动剖析(二)
  • ​sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块​
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • #pragma pack(1)
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • (1) caustics\
  • (16)UiBot:智能化软件机器人(以头歌抓取课程数据为例)
  • (30)数组元素和与数字和的绝对差
  • (4)(4.6) Triducer
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (DFS + 剪枝)【洛谷P1731】 [NOI1999] 生日蛋糕
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (八)Flask之app.route装饰器函数的参数
  • (附源码)基于SSM多源异构数据关联技术构建智能校园-计算机毕设 64366
  • (规划)24届春招和25届暑假实习路线准备规划
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (三)Pytorch快速搭建卷积神经网络模型实现手写数字识别(代码+详细注解)
  • (一)RocketMQ初步认识
  • .NET CLR Hosting 简介
  • .NET 程序如何获取图片的宽高(框架自带多种方法的不同性能)
  • .NET 指南:抽象化实现的基类