php 如何在创建对象时为 stdClass 对象设置属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13761335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:01:17  来源:igfitidea点击:

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

phpstdclass

提问by Habeeb Perwad

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

我想在单个语句中为stdClass对象设置属性。我对此一无所知。我知道以下几点

$obj = new stdClass;
$obj->attr = 'loremipsum';

It takes two statements.

它需要两个语句。

$obj = (object) array('attr'=>'loremipsum');

It takes single statement but it is not direct method.

它需要单个语句,但不是直接方法。

$obj = new stdClass(array('attr'=>'loremipsum'));

It is not working.

它不工作。

回答by Ja?ck

$obj = (object) array(
    'attr'=>'loremipsum'
);

Actually, that's as direct as it's going to get. Even a custom constructor won't be able to do this in a single expression.

实际上,这很直接。即使是自定义构造函数也无法在单个表达式中执行此操作。

The (object)cast mightactually be a simple translation from an array, because internally the properties are stored in a hash as well.

(object)可能实际上是从一个数组简单的翻译,因为内部的属性存储在一个哈希为好。

You could create a base class like this:

你可以像这样创建一个基类:

abstract class MyObject
{
    public function __construct(array $attributes = array())
    {
        foreach ($attributes as $name => $value) {
            $this->{$name} = $value;
        }
    }
}

class MyWhatever extends MyObject
{
}

$x = new MyWhatever(array(
    'attr' => 'loremipsum',
));

Doing so will lock up your constructor though, requiring each class to call its parent constructor when overridden.

这样做会锁定你的构造函数,要求每个类在被覆盖时调用它的父构造函数。

回答by dotancohen

Though Ja?ck gives a good answer, it is important to stress that the PHP interpreter itself has a method for describing how to properly represent an object or variable:

尽管 Hyman 给出了一个很好的答案,但重要的是要强调 PHP 解释器本身有一种方法来描述如何正确表示对象或变量

php > $someObject = new stdClass();
php > $someObject->name = 'Ethan';
php > var_export($someObject);
stdClass::__set_state(array(
   'name' => 'Ethan',
))

Interestingly, using stdClass::__set_statefails to create a stdClass object, thus displaying it as such is likely a bug in var_export(). However, it does illustrate that there is no straightforward method to create the stdClass object with attributes set at the time of object creation.

有趣的是,使用stdClass::__set_state无法创建stdClass的对象,从而显示它是这样很可能中的一个错误var_export()但是,它确实说明没有直接的方法来创建具有在对象创建时设置的属性的 stdClass 对象。

回答by Hydtek

        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.

是最干净的,因为如果属性不存在,它会在返回对象的 print_r(get_object_vars($obj)) 中设置任意属性。