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

    ?>

  • 相关阅读:
    模式使用详解 手拉手就是职责链吗?
    简单说两句味道
    胡说两句...
    补充说明: 表驱动, 链表与职责链
    C++/CLI和TMP: enum hack相當於static const int吗?
    WCF NetTcpBinding Transport安全模式(9) ClientCredentialType证书验证模式Custom验证模式
    WCF NetTcpBinding Transport安全模式(8) ClientCredentialType证书验证模式 PeerOrChainTrust验证模式
    WCF NetTcpBinding Transport安全模式(2) 默认安全配置
    WCF NetTcpBinding Transport安全模式(7) ClientCredentialType证书验证模式 ChainTrust验证模式
    WCF NetTcpBinding Transport安全模式(6) ClientCredentialType证书验证模式 PeerTrust验证模式
  • 原文地址:https://www.cnblogs.com/oxspirt/p/14266842.html
Copyright © 2020-2023  润新知