Examples of two implementations of the PHP container class

  • 2021-12-13 16:29:12
  • OfStack

This paper illustrates two implementations of PHP container class. Share it for your reference, as follows:

Achieved by magic method

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');

Realized through ArrayAccess interface

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');

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: