Home> php教程> php手册> body text

在PHP中像数组一样访问Obj,以及性能测试

WBOY
Release: 2016-06-06 20:09:43
Original
1074 people have browsed it

前两天看了下PHP里面的一些接口(interface),就写了一些简单的代码测试一下。 http://leo108.com/pid-1917.asp 在PHP的各种MVC框架中,通常会有一个Model类,用来进行CURD操作,这个类会把对应数据库中的数据作为一个数组存放在一个私有的属性里,然后通过__

前两天看了下PHP里面的一些接口(interface),就写了一些简单的代码测试一下。

http://leo108.com/pid-1917.asp

在PHP的各种MVC框架中,通常会有一个Model类,用来进行CURD操作,这个类会把对应数据库中的数据作为一个数组存放在一个私有的属性里,然后通过__get和__set来存取。大概样子如下:

未经允许严禁转载

class Model { private $data; public function __construct($arr){ $this->data = $arr; } public function __get($key){ return $this->data[$key]; } public function __set($key,$value) { $this->data[$key] = $value; } }
Copy after login

首先要实现用数组的方式来访问对象,需要继承ArrayAccess接口,代码如下:未经允许严禁转载

class Model implements ArrayAccess{ private $data; public function __construct($arr){ $this->data = $arr; } /* __get和__set代码略 */ /** * 实现ArrayAccess接口offsetExists * @access public * @param string $key 名称 * @return bool */ public function offsetExists($key){ return isset($this->data[$key]); } /** * 实现ArrayAccess接口offsetGet * @access public * @param string $key 名称 * @return mixed 值 */ public function offsetGet($key){ return $this->data[$key]; } /** * 实现ArrayAccess接口offsetSet * @access public * @param string $key 名称 * @param mixed $value 值 * @return void */ public function offsetSet($key,$value){ $this->data[$key] = $value; } /** * 实现ArrayAccess接口offsetUnset * @access public * @param string $key 名称 * @return void */ public function offsetUnset($key){ unset($this->data[$key]); } }
Copy after login

使用方法是:

PHP

$testObj = new Model(array('a'=>1,'b'=>2,'c'=>3)); echo $testObj['a']; //输出1
Copy after login

然后要让Obj能用foreach的方式访问,一种方式是使用Iterator接口,并且实现Iterator接口所需要的几个方式,这个比较繁琐。PHP

因为我们要访问的数据都在data属性中,可以用IteratorAggregate接口和ArrayIterator迭代器来实现,具体原理请百度。

在PHP中像数组一样访问Obj,以及性能测试

class Model implements IteratorAggregate,ArrayAccess{ //这里增加了一个接口 private $arr; public function __construct($arr){ $this->data = $arr; } public function getIterator(){ return new ArrayIterator($this->data); } /* __get和__set代码略 */ /* 实现ArrayAccess的几个方法略 */ }
Copy after login

然后就可以用foreach来访问了。本文来自leo108's blog

但是这会带来性能问题,我在自己的服务器上简单测试了下,用一个for循环1000000次来访问data中的值,然后用ab分别压力测试100次,结果如下:

PHP

type time
1 0.83729560136796
2 0.77318458318708
3 0.50159471273426

测试1就是用我实现的Model类,然后用数组的方式取值

本文来自http://leo108.com

测试2也是用我实现的Model类,用__get方式取值在PHP中像数组一样访问Obj,以及性能测试

测试3用的是没有实现接口的类,用__get方式取值

PHP

很明显的结果,用了接口之后会严重影响性能,因此只能在对性能要求不高的场景使用

本文来自http://leo108.com

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!