php系统转换的三种方式(PHP容器类的两种实现方式示例)
类别:编程学习 浏览量:257
时间:2022-01-17 01:06:06 php系统转换的三种方式
PHP容器类的两种实现方式示例本文实例讲述了PHP容器类的两种实现方式。分享给大家供大家参考,具体如下:
通过魔术方法实现
class
|
class MagicContainer{ private $ele ; function __construct() { $this ->ele = []; } function __set( $name , $value ) { $this ->ele[ $name ] = $value ; } function __get( $name ) { return $this ->ele[ $name ]; } function __isset( $name ) { return isset( $this ->ele[ $name ]); } function __unset( $name ) { if (isset( $this ->ele[ $name ])){ unset( $this ->ele[ $name ]); } } } |
usage
|
$container = new MagicContainer(); $container ->logger = function ( $msg ){ file_put_contents ( 'info.log' , $msg .PHP_EOL,FILE_APPEND); }; $logger = $container ->logger; $logger ( 'magic container works' ); |
通过ArrayAccess接口实现
class
|
class ArrayContainer implements ArrayAccess { private $elements ; public function __construct() { $this ->elements = []; } public function offsetExists( $offset ){ return isset( $this ->elements[ $offset ]); } public function offsetGet( $offset ){ if ( $this ->offsetExists( $offset )){ return $this ->elements[ $offset ]; } else { return false; } } public function offsetSet( $offset , $value ){ $this ->elements[ $offset ] = $value ; } public function offsetUnset( $offset ){ if ( $this ->offsetExists( $offset )){ unset( $this ->elements[ $offset ]); } } } |
usage
|
$container = new ArrayContainer(); $container [ 'logger' ] = function ( $msg ){ file_put_contents ( 'info.log' , $msg .PHP_EOL,FILE_APPEND); }; $logger = $container [ 'logger' ]; $logger ( 'array container works' ); |
Container
class
|
class Container implements ArrayAccess { private $elements ; public function __construct() { $this ->elements = []; } public function offsetExists( $offset ){ return isset( $this ->elements[ $offset ]); } public function offsetGet( $offset ){ if ( $this ->offsetExists( $offset )){ return $this ->elements[ $offset ]; } else { return false; } } public function offsetSet( $offset , $value ){ $this ->elements[ $offset ] = $value ; } public function offsetUnset( $offset ){ if ( $this ->offsetExists( $offset )){ unset( $this ->elements[ $offset ]); } } function __set( $name , $value ) { $this ->elements[ $name ] = $value ; } function __get( $name ) { return $this ->elements[ $name ]; } function __isset( $name ) { return isset( $this ->elements[ $name ]); } function __unset( $name ) { if (isset( $this ->elements[ $name ])){ unset( $this ->elements[ $name ]); } } } |
usage
|
$container = new Container(); $container [ 'logger' ] = function ( $msg ){ file_put_contents ( 'info.log' , $msg .PHP_EOL,FILE_APPEND); }; $logger = $container ->logger; $logger ( 'container works' ); |
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/koastal/article/details/72528456
您可能感兴趣
- php网站经典案例(PHP生成短网址的思路以及实现方法的详解)
- php中trim函数(PHP中ltrim函数的用法与实例讲解)
- dedecms注册怎么给权限(织梦DedeCms系统未审核文档禁止动态浏览修改方法view.php)
- dedecms的dedesql.class.php on line 489错误的解决方法(dedecms的dedesql.class.php on line 489错误的解决方法)
- php开发的主要技术(详解PHP神奇又有用的Trait)
- php复选框代码(PHP之多条件混合筛选功能的实现方法)
- 织梦cms中的模块怎么设置(织梦CMS模板在runphp=yes的标签中调用其他field值的方法)
- php检查语法(使用 PHP Masked Package 屏蔽敏感数据的实现方法)
- web项目部署到阿里云服务器上(阿里云的WindowsServer2016上部署php+apache)
- php逻辑技巧图解(php版本CKEditor 4和CKFinder安装及配置方法图文教程)
- phpstudy服务器怎么设置(phpstudy2018 访问目录服务权限问题)
- 常见的php五大运行模式详解(php设计模式之职责链模式定义与用法经典示例)
- php 十六进制转换为二进制(PHP实现的62进制转10进制,10进制转62进制函数示例)
- thinkphp5设计权限(Thinkphp5.0 框架视图view的比较标签用法分析)
- php 与asp对比(asp.net和php的区别点总结)
- php函数使用教程讲解学习交流(浅谈PHP中的Trait使用方法)
- 春节会放假几天(春节会放假吗)
- 小浴室,大民生 缙云3200多户困难群众洗上免费热水澡(小浴室大民生缙云3200多户困难群众洗上免费热水澡)
- 元旦闲谭(元旦闲谭)
- 息烽 这个村 治垃圾 有招 人人争当卫生模范(息烽这个村治垃圾)
- 今天要吃什么(今天要吃什么菜)
- 三杨之一 南杨 杨溥 安贞履节,酿醴调羹,宰相之气(三杨之一南杨杨溥)
热门推荐
- 宝塔面板错误代码3(宝塔面板打开网站No input file specified如何解决?)
- 带农历的js日期控件
- dockerpull的镜像位置(解决docker pull镜像报错的问题)
- phpstudy使用教程最新版(phpstudy安装使用图文教程)
- Javascript操作DOM
- linuxread指令(Linux read命令的使用)
- python中encode中文自定义编码(详解Python解决抓取内容乱码问题decode和encode解码)
- python内置函数使用方法(Python神奇的内置函数locals的实例讲解)
- pythonrequest包设置编码(解决python3中的requests解析中文页面出现乱码问题)
- docker 命令上传镜像到镜像仓库(Docker 使用国内镜像仓库的方法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9