php 从对象中删除成员?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5359083/
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 21:09:01  来源:igfitidea点击:

Remove a member from an object?

phpclassattributes

提问by rinogo

Is there a simple way to remove a member from an object? Not just set it to null, but actually remove it.

有没有一种简单的方法可以从对象中删除成员?不只是将其设置为 null,而是实际将其删除。

Thanks! :)

谢谢!:)

Edit: I've already tried unset(), and setting the member variable to null obviously doesn't work. I suppose I could convert the object to an array, then remove the array key in question, and convert back to an object, but blech... There's got to be an easier way!

编辑:我已经尝试过 unset(),并且将成员变量设置为 null 显然不起作用。我想我可以将对象转换为数组,然后删除有问题的数组键,然后转换回对象,但是 blech ......必须有一种更简单的方法!

回答by mario

You are using RedBean. Just checked it out. And these bean objects don't have actual properties.

您正在使用 RedBean。刚刚查了一下。而且这些 bean 对象没有实际属性。

unset($bean->field);

Does not work, because ->fieldis a virtual attribute. It does not exist in the class. Rather it resides in the protected $bean->properties[]which you cannot access. RedBean only implements the magic methods __getand __setfor retrieving and setting attributes.

不起作用,因为->field虚拟属性。它在类中不存在。相反,它驻留在protected $bean->properties[]您无法访问的地方。RedBean 仅实现魔术方法__get以及__set检索和设置属性。

This is why the unset()does not work. It unsets a property that never existed at that location.

这就是为什么unset()不起作用。它取消设置该位置从未存在的属性。

回答by Jon

$obj = new stdClass;
$obj->answer = 42;
print_r($obj);
unset($obj->answer);
print_r($obj);

Works fine for me. Are you sure you 're doing it right?

对我来说很好用。你确定你做对了吗?

Update:

更新:

It also worksfor properties defined in classes:

它也适用于类中定义的属性:

class Foo {
    public $bar = 42;
}

$obj = new Foo;
print_r($obj);
unset($obj->bar);
print_r($obj);

回答by RobertPitt

within you object you can define a magic method called __unset

在您的对象中,您可以定义一个名为 __unset

class Test
{
    public $data = array();

    public function __unset($key)
    {
        unset($this->data[$key]);
    }
}

And Jon summed up the other factors nicely.

乔恩很好地总结了其他因素。

回答by user3410818

RedBean has a removeProperty method on beans.

RedBean 有一个关于 bean 的 removeProperty 方法。

回答by m13r

With RedBean 4 you can use

使用 RedBean 4,您可以使用

unset($bean->someproperty);

回答by Prisoner

Possibly unset().

可能是unset()

回答by mhitza

No you cannot, nor in the Runkit module do I see a way to accomplish that, even if ways to remove methods/functions/constantsexist.

不,你不能,在 Runkit 模块中我也没有看到一种方法来实现这一点,即使存在删除方法/函数/常量的方法

回答by Fabien Snauwaert

Do you want to unset the property merely because you do not want it stored in the database?

您是否仅仅因为不想将其存储在数据库中而想取消设置该属性

If so, just declare the property as privatein the class.

如果是这样,只需像private在类中一样声明该属性。

Kudos to this answer: Not saving property of a php-redbean to database

对此答案表示赞赏未将 php-redbean 的属性保存到数据库