开发者

Do I need to release the object in php?

Previously I've used Objective-C in iPhone Development. It seems I need to do some work in the back-end s开发者_JS百科ite, I chose PHP for this. In Objective-C, I need to release objects after I use them. Do I need to do this in PHP also? Thank you.


99.9% of the time, no you do not need to delete the object. PHP has a garbage collector that will handle that for you. The garbage collector will free a given instance of memory once it is no longer needed in your code.

To read more:

http://php.net/manual/en/features.gc.php


You don't except for persistent database connections.

http://php.net/manual/en/language.types.resource.php


Not for memory, PHP uses garbage collection. Objects are released at some point after they become unreachable (not instantly as some simplified introductions claim) to your code. This includes reference cycles since a few versions (a references b, b references a) - previously, it used reference counting only and thus failed in the face of such cycles.

Resources (files, database connections, etc.) might happen to be closed when they are collected, but since the time of collection is non-deterministic and whether that closes them depends on the resource in question, it's propably a very bad idea to just leave them when you're done with them.


No. You don't! Php has a garbage collector that take care of memory handling for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜