• 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
    */

    ?>

  • 相关阅读:
    java多线程(待完善)
    eclipse console 查看全部的输出
    maven仓库地址
    拷贝Maven工程依赖的jar包出来
    ElasticSearch
    python2学习------基础语法5(常用容器以及相关操作)
    文本框焦点事件改变默认文字
    随机更换图片
    妙味——JS数组的方法
    妙味——封装getStyle()获取样式
  • 原文地址:https://www.cnblogs.com/oxspirt/p/14266842.html
Copyright © 2020-2023  润新知