How to set attributes for stdClass object at the time object creation

后端 未结 3 870
一个人的身影
一个人的身影 2021-02-03 18:40

I want to set attribute for a stdClass object in a single statement. I don\'t have any idea about it. I know the following things

$obj = new stdClass;
$         


        
3条回答
  •  后悔当初
    2021-02-03 18:54

            foreach ($attributes as $name => $value) {
                if (property_exists(self::class, $name)) {
                    $this->{$name} = $value;
                }
            }
    

    is cleanest because it will set an arbitrary attribute if you print_r(get_object_vars($obj)) of returned object if attribute does not exist.

提交回复
热议问题