PHP容器类的两种实现方式示例

时间:2022-08-30 19:04:41

本文实例讲述了PHP容器类的两种实现方式。分享给大家供大家参考,具体如下:

通过魔术方法实现

class

?
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
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

?
1
2
3
4
5
6
$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

?
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
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

?
1
2
3
4
5
6
$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

?
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
42
43
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

?
1
2
3
4
5
6
$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