Initialize Objects like arrays in PHP?

后端 未结 8 1477
情话喂你
情话喂你 2021-01-01 09:04

In PHP, you can initialize arrays with values quickly using the following notation:

$array = array(\"name\" => \"member 1\", array(\"name\" => \"member         


        
8条回答
  •  难免孤独
    2021-01-01 09:59

    I use a class I name Dict:

    class Dict {
    
        public function __construct($values = array()) {
            foreach($values as $k => $v) {
                $this->{$k} = $v;
            }
        }
    }
    

    It also has functions for merging with other objects and arrays, but that's kinda out of the scope of this question.

提交回复
热议问题