can i use stdClass like an array?

后端 未结 6 900
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-23 08:21

is it possible to make stdClass objects work like a generically indexed array?

i.e. $array = Array ( [0] => 120 [1] => 382 [2] => 552 [3] => 595 [4] => 616 )

wou

6条回答
  •  悲&欢浪女
    2021-01-23 08:36

    Only for the sake of OOP, don't use classes. If you really want to implement this functionality, use this class:

    class ArrayClass
    {
        protected $storage = array();
    
        public function push($content) {
            $this->storage[] = $content;
            return $this;
        }
    
        public function get($index) {
            if (isset($this->storage[$index])) {
                return $this->storage[$index];
            } else {
                //throw an error or
                return false;
            }
        }
    }
    

提交回复
热议问题