如何取消设置对象的继承属性?

发布于 2024-08-27 18:50:17 字数 539 浏览 11 评论 0原文

我有一个对象

    (
        [id] => 1
        [parent_id] => 0
        [result:Database:private] => 
        [db:Database:private] => mysqli Object
            (
                [affected_rows] => 0
                ...
            )

    )

显然,该对象继承了父数据库类的“db”和“结果”属性。

unset($object->result) 或 unset($object->result:Database) 或 unset($object->result:Database:private) 都有效。

当不再需要这些属性时(即当对象属性即将输出时),我如何取消设置它们?

将数据库对象作为其他类的继承属性(使用数据库连接的所有其他类扩展一个数据库类)通常是一个好主意吗?

I have an Object

    (
        [id] => 1
        [parent_id] => 0
        [result:Database:private] => 
        [db:Database:private] => mysqli Object
            (
                [affected_rows] => 0
                ...
            )

    )

Obviously, the Object has inherited the 'db' and 'result' properties of the parent Database class.

unset($object->result) nor unset($object->result:Database) nor unset($object->result:Database:private) work.

How could I unset those properties when they are no longer needed (i.e. when the object properties are about to be output)?

Is it a generally a good idea to have a database object as an inherited property of other classes (extend one Database class with all other classes that use database connections)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

另类 2024-09-03 18:50:18

如果您仅在一个对象中使用 unset() ,则不会取消该对象的设置,因为它存在于内存中的某处,而对象中的内容只是一个引用。持有此数据库对象的所有其他对象都使用相同的引用。您唯一能做的就是将此引用设置为等于 NULL,但您不会释放任何内存,因为数据库仍然被其他对象使用。

一般来说,我不会担心取消设置数据库对象,因为您不会获得任何性能提升。当执行结束时,PHP 将自动垃圾收集其对象。

是的,拥有一个静态数据库对象或在使用数据库的类中继承它是一个好主意。

If you are using unset() only in one object you are not unsetting the object, because it exists in the memory somewhere and what you have in your object is just a reference. The same reference is used by all you other objects holding this DB object. The only thing you can do is to set this reference to equal NULL, but you are not freeing any memory since the DB is still there used by other objects.

Generally I would not worry about unsetting the DB object as you will not get any performance boost. PHP will automatically garbage collect its objects when the execution ends.

Yes, it is a good idea to have one static DB object or inherit it in your classes that are using the database.

盛夏尉蓝 2024-09-03 18:50:17

尝试$object->result = null

将数据库对象作为其他类的继承属性(将一个数据库类扩展为使用数据库连接的所有其他类)通常是一个好主意吗?

不。您的班级最好只承担一项责任。将数据库连接保存在一个类或一组类中,其职责是从数据库中读取和写入数据。您的其他域对象应该处理自己的职责,而不会被 SQL 和数据库代码拥挤 - 即使它是继承的。

您可能还想了解“优先考虑组合而不是继承”的概念。

Try $object->result = null

Is it a generally a good idea to have a database object as an inherited property of other classes (extend one Database class with all other classes that use database connections)?

No. It is best that your class have one responsibility. Keep your database connections in a class or set of classes whose responsibility is to read and write data from the database. Your other domain objects should handle their own responsibilities without being crowded by SQL and database code - even if it is inherited.

You may also want to read up on the concept of "favor composition over inheritance."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文