zoukankan      html  css  js  c++  java
  • php offsetGet 像访问数组一样访问对象

     https://www.php.net/manual/en/class.arrayaccess.php

    --------------------------

    <?php
    
    /**
    * ArrayAndObjectAccess
    * Yes you can access class as array and the same time as object
    *
    * @author Yousef Ismaeil <cliprz@gmail.com>
    */
    
    class ArrayAndObjectAccess implements ArrayAccess {
    
        /**
         * Data
         *
         * @var array
         * @access private
         */
        private $data = [];
    
        /**
         * Get a data by key
         *
         * @param string The key data to retrieve
         * @access public
         */
        public function &__get ($key) {
            return $this->data[$key];
        }
    
        /**
         * Assigns a value to the specified data
         * 
         * @param string The data key to assign the value to
         * @param mixed  The value to set
         * @access public 
         */
        public function __set($key,$value) {
            $this->data[$key] = $value;
        }
    
        /**
         * Whether or not an data exists by key
         *
         * @param string An data key to check for
         * @access public
         * @return boolean
         * @abstracting ArrayAccess
         */
        public function __isset ($key) {
            return isset($this->data[$key]);
        }
    
        /**
         * Unsets an data by key
         *
         * @param string The key to unset
         * @access public
         */
        public function __unset($key) {
            unset($this->data[$key]);
        }
    
        /**
         * Assigns a value to the specified offset
         *
         * @param string The offset to assign the value to
         * @param mixed  The value to set
         * @access public
         * @abstracting ArrayAccess
         */
        public function offsetSet($offset,$value) {
            if (is_null($offset)) {
                $this->data[] = $value;
            } else {
                $this->data[$offset] = $value;
            }
        }
    
        /**
         * Whether or not an offset exists
         *
         * @param string An offset to check for
         * @access public
         * @return boolean
         * @abstracting ArrayAccess
         */
        public function offsetExists($offset) {
            return isset($this->data[$offset]);
        }
    
        /**
         * Unsets an offset
         *
         * @param string The offset to unset
         * @access public
         * @abstracting ArrayAccess
         */
        public function offsetUnset($offset) {
            if ($this->offsetExists($offset)) {
                unset($this->data[$offset]);
            }
        }
    
        /**
         * Returns the value at specified offset
         *
         * @param string The offset to retrieve
         * @access public
         * @return mixed
         * @abstracting ArrayAccess
         */
        public function offsetGet($offset) {
            return $this->offsetExists($offset) ? $this->data[$offset] : null;
        }
    
    }
    
    
    $foo = new ArrayAndObjectAccess();
    // Set data as array and object
    $foo->fname = 'Yousef';
    $foo->lname = 'Ismaeil';
    // Call as object
    echo 'fname as object '.$foo->fname."
    ";
    // Call as array
    echo 'lname as array '.$foo['lname']."
    ";
    // Reset as array
    $foo['fname'] = 'Cliprz';
    echo $foo['fname']."
    ";
    
    /** Outputs
    fname as object Yousef
    lname as array Ismaeil
    Cliprz
    */
    
    
    

      

    <?php

    /**
    * ArrayAndObjectAccess
    * Yes you can access class as array and the same time as object
    *
    * @author Yousef Ismaeil <cliprz@gmail.com>
    */

    class ArrayAndObjectAccess implements ArrayAccess {

        
    /**
         * Data
         *
         * @var array
         * @access private
         */
        
    private $data = [];

        
    /**
         * Get a data by key
         *
         * @param string The key data to retrieve
         * @access public
         */
        
    public function &__get ($key) {
            return 
    $this->data[$key];
        }

        
    /**
         * Assigns a value to the specified data
         * 
         * @param string The data key to assign the value to
         * @param mixed  The value to set
         * @access public 
         */
        
    public function __set($key,$value) {
            
    $this->data[$key] = $value;
        }

        
    /**
         * Whether or not an data exists by key
         *
         * @param string An data key to check for
         * @access public
         * @return boolean
         * @abstracting ArrayAccess
         */
        
    public function __isset ($key) {
            return isset(
    $this->data[$key]);
        }

        
    /**
         * Unsets an data by key
         *
         * @param string The key to unset
         * @access public
         */
        
    public function __unset($key) {
            unset(
    $this->data[$key]);
        }

        
    /**
         * Assigns a value to the specified offset
         *
         * @param string The offset to assign the value to
         * @param mixed  The value to set
         * @access public
         * @abstracting ArrayAccess
         */
        
    public function offsetSet($offset,$value) {
            if (
    is_null($offset)) {
                
    $this->data[] = $value;
            } else {
                
    $this->data[$offset] = $value;
            }
        }

        
    /**
         * Whether or not an offset exists
         *
         * @param string An offset to check for
         * @access public
         * @return boolean
         * @abstracting ArrayAccess
         */
        
    public function offsetExists($offset) {
            return isset(
    $this->data[$offset]);
        }

        
    /**
         * Unsets an offset
         *
         * @param string The offset to unset
         * @access public
         * @abstracting ArrayAccess
         */
        
    public function offsetUnset($offset) {
            if (
    $this->offsetExists($offset)) {
                unset(
    $this->data[$offset]);
            }
        }

        
    /**
         * Returns the value at specified offset
         *
         * @param string The offset to retrieve
         * @access public
         * @return mixed
         * @abstracting ArrayAccess
         */
        
    public function offsetGet($offset) {
            return 
    $this->offsetExists($offset) ? $this->data[$offset] : null;
        }

    }

    ?>

    Usage

    <?php
    $foo 
    = new ArrayAndObjectAccess();
    // Set data as array and object
    $foo->fname 'Yousef';
    $foo->lname 'Ismaeil';
    // Call as object
    echo 'fname as object '.$foo->fname." ";
    // Call as array
    echo 'lname as array '.$foo['lname']." ";
    // Reset as array
    $foo['fname'] = 'Cliprz';
    echo 
    $foo['fname']." ";

    /** Outputs
    fname as object Yousef
    lname as array Ismaeil
    Cliprz
    */

    ?>

  • 相关阅读:
    shell 知识点
    辅助字符串处理类:org.apache.commons.lang3.StringUtils
    post请求(headers里有属性)报错:Request header field xxx is not allowed by Access-Control-Allow-Headers in preflight response
    vue-cli 打包报错:Unexpected token: punc (()
    遍历对象,并对其中第一个(随机)进行处理
    JavaScript中类似PHP的uniqid()方法
    使用crypto-js的md5加密
    Yarn、MapReduce、spark、storm的关系
    hadoop 知识点
    spring cloud 知识点
  • 原文地址:https://www.cnblogs.com/oxspirt/p/14266842.html
Copyright © 2011-2022 走看看