Initialize Objects like arrays in PHP?

后端 未结 8 1472
情话喂你
情话喂你 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

    from this answer to a similar question:

    As of PHP7, we have Anonymous Classes which would allow you to extend a class at runtime, including setting of additional properties:

    $a = new class() extends MyObject {
        public $property1 = 1;
        public $property2 = 2;
    };
    
    echo $a->property1; // prints 1
    

    It's not as succinct as the initializer for array. Not sure if I'd use it. But it is another option you can consider.

提交回复
热议问题