突然想整理下自己的技术栈,毕竟也工作的蛮久,很多东西可能一直处于盲区所以想统一的做一个归纳,不懂的进一步去学习下,完善一下自己的技术栈。

突然想整理下自己的技术栈,毕竟也工作的蛮久,很多东西可能一直处于盲区所以想统一的做一个归纳,不懂的进一步去学习下,完善一下自己的技术栈。
php是工作中用的最多的语言,但是觉大多数的时候我们都是写一些逻辑代码,高级的知识点我们接触使用的并不是很多,php的整体归纳为第一个要攻下的目标。php的spl里面有很多有用的东西,之前用过一些但是还是有很多没用尝试过的,接下来就先来使用下一些很有用的spl及使用场景。

ArrayAccess

ArrayAccess是数组式访问接口,提供了以下接口:

1
2
3
4
5
6
7
ArrayAccess {
/* 方法 */
abstract public boolean offsetExists ( mixed $offset )
abstract public mixed offsetGet ( mixed $offset )
abstract public void offsetSet ( mixed $offset , mixed $value )
abstract public void offsetUnset ( mixed $offset )
}

结合配置文件的使用场景我们来使用下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php

require_once(dirname(__FILE__).'/vendor/autoload.php');

final Class Config implements \ArrayAccess {
public $config = [];
private $path = null;

public function __construct()
{
$this->path = __DIR__ . '/../../../shihuo_v36/config/';
}
public function offsetExists($offset)
{
return isset($this->config[$offset]);
}
public function offsetGet($offset)
{
if (!isset($this->config[$offset])) {
$ymlPath = $this->path . $offset . '.yml';

$data = spyc_load_file($ymlPath);

$this->config[$offset] = $data;
}
return $this->config[$offset];
}
public function offsetSet($offset, $value)
{
$this->config[$offset] = $value;
}
public function offsetUnset($offset)
{
}
}

$c = new Config();

print_r($c['app']['prod']['.api']);
print_r($c['databases']['prod']['mysphinxMysql']);