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

php memcached存储对象,从memcached获取对象并在PHP中设置为self

你的问题和后续评论的措辞有些含糊不清,但他们仍然指出我的方向如下:

public function __construct($id) {

global $pdo, $memcached;

$data = $memcached->get($id);

if($memcached->getResultCode() == Memcached::RES_SUCCESS) {

// this is not currently allowed in PHP

$this = $data;

// this should be your fix

foreach($data AS $key => $value) {

$this->$key = $value;

}

// or this

foreach($this AS $key => $value) {

$this->$key = $data[$key];

}

// the difference between the fixes above is that

// the second is strictly limited to values defined

// by the class (current object)

}

else {

$pdos = $pdo->prepare('SELECT * FROM table_name WHERE id = ?');

if($pdos) {

// this is not allowed in PHP

$pdos->execute(array(intval($id)));

$this = $pdos->fetch(PDO::FETCH_CLASS, get_class($this));

// all of this should work fine and is allowed

$pdos->setFetchMode(PDO::FETCH_INTO, $this);

$pdos->execute(array(intval($id)));

$pdos->fetch(PDO::FETCH_INTO);

}

}

}但不幸的是,PHP不允许在内部覆盖$ this的值(在它自己的方法调用中),因此可以做的另一种方法是使用静态方法。

public static function getByID($id) {

global $pdo, $memcached;

$data = $memcached->get($id);

if($memcached->getResultCode() == Memcached::RES_SUCCESS) {

// this will work if your objects construct has a

// foreach similar to the ones presented above

$result = new self($data);

// or if you don't want to write a foreach in

// the construct you can have it here

foreach($data AS $key => $value) {

$this->$key = $value;

}

// or here

foreach($this AS $key => $value) {

$this->$key = $data[$key];

}

}

else {

$pdos = $pdo->prepare('SELECT * FROM table_name WHERE id = ?');

if($pdos) {

// either of these should work

$pdos->execute(array(intval($id)));

$result = $pdos->fetch(PDO::FETCH_CLASS, get_class($this));

// either of these should work

$result = new self;

$pdos->setFetchMode(PDO::FETCH_INTO, $result);

$pdos->execute(array(intval($id)));

$pdos->fetch(PDO::FETCH_INTO);

}

}

return($result);

}用法语法为MyClass::get($some_id)。

相关文章:

  • java阅读安卓,java – 如何在android中逐行阅读?
  • plotm matlab,MATLAB画地图的工具:worldmap和m_map
  • matlab不能盗版吗,matlab为了防止盗版,会不会篡改程序运行结果?这是明证
  • matlab 复权数据,〖Matlab〗基于通达信股价数据的复权处理(fantuanxiaot版本)
  • 微信小程序向php传递数据,微信小程序 跳转传递数据的方法
  • mysql8默认存储,MySQL 8.0安装
  • php berkeleydb,berkeleydb-5.1
  • matlab vs 打包exe文件路径,Matlab中調用VS編譯的exe文件並傳遞變量 的方法
  • php 访问受保护的属性,php – 我们应该直接访问受保护的属性还是使用getter?
  • PHP限制请求类型,Laravel :API 请求频率限制(Throttle中间件),自定义返回JSON类型,自定义时间...
  • oracle归档增长快,归档日志增长过快的问题
  • oracle 行转列 顺序,Oracle PIVOT 行转列的单行小计如何实现?
  • oracle asm安装详情,oracle ASM安装记录
  • oracle in _all和any的区别,英语all和any的用法区别
  • linux用户隔离,CentOS下实现用户隔离模式的SFTP
  • 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE
  • 4. 路由到控制器 - Laravel从零开始教程
  • ES6核心特性
  • JAVA_NIO系列——Channel和Buffer详解
  • Javascript编码规范
  • JavaWeb(学习笔记二)
  • JAVA多线程机制解析-volatilesynchronized
  • Mac转Windows的拯救指南
  • ReactNativeweexDeviceOne对比
  • springboot_database项目介绍
  • vue-cli在webpack的配置文件探究
  • windows下使用nginx调试简介
  • 初识MongoDB分片
  • 前端面试之闭包
  • 什么软件可以提取视频中的音频制作成手机铃声
  • 使用 5W1H 写出高可读的 Git Commit Message
  • 世界上最简单的无等待算法(getAndIncrement)
  • 走向全栈之MongoDB的使用
  • Java总结 - String - 这篇请使劲喷我
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • #[Composer学习笔记]Part1:安装composer并通过composer创建一个项目
  • #include到底该写在哪
  • $.ajax()
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (Ruby)Ubuntu12.04安装Rails环境
  • (备忘)Java Map 遍历
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (附源码)springboot家庭财务分析系统 毕业设计641323
  • (一)u-boot-nand.bin的下载
  • (转载)PyTorch代码规范最佳实践和样式指南
  • ***利用Ms05002溢出找“肉鸡
  • .naturalWidth 和naturalHeight属性,
  • .NET Core WebAPI中使用swagger版本控制,添加注释
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .NET Core 中的路径问题
  • .NET 设计模式—适配器模式(Adapter Pattern)
  • .Net6支持的操作系统版本(.net8已来,你还在用.netframework4.5吗)
  • .net图片验证码生成、点击刷新及验证输入是否正确
  • @RestControllerAdvice异常统一处理类失效原因
  • @selector(..)警告提示