Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to destroy objects in PHP after using them?

Tags:

object

php

How crucial is object destruction in PHP? is it important to destroy objects in PHP after using them? because unlike java, PHP doesn't have a garbage collector (nothing that I know of)

like image 670
user962206 Avatar asked Jun 23 '12 15:06

user962206


People also ask

How does PHP remove unused objects?

Using the PHP unset() function, we can delete an object. So with the PHP unset() function, putting the object that we want to delete as the parameter to this function, we can delete this object. This is shown below. So in the above code, we delete an object named $object1.

How do you delete an object in PHP?

// Method 1: Set to null $var = null; // Method 2: Unset unset($var);

Why do we need objects in PHP?

In PHP, Object is a compound data type (along with arrays). Values of more than one types can be stored together in a single variable. Object is an instance of either a built-in or user defined class. In addition to properties, class defines functionality associated with data.

Which method is used to destroy the object?

Only the garbage collection system can destroy an object.


2 Answers

You don't need to destroy objects in the general case, and PHP certainly does have a garbage collector. Moreover, most simple scripts would not even really need one because the whole environment is torn down and rebuilt for every HTTP request; the garbage collector helps those scripts that would run out of memory while serving a single request.

Exceptions to the general case:

You might want to "lose" all references to objects that consume a large amount of memory and/or wrap unmanaged resources; this is usually as easy as

$largeObject = null; // reference to previous value lost

If that was the last reference to $largeObject, then:

  • PHP will immediately call the destructor (if one exists); you might want to do for objects which are wrapping unmanaged resources (e.g. database connections, sockets, etc) if your script is long-lived.
  • The memory that the object takes up will now be eligible for garbage collection. However, collection will not occur on the spot but at some later point where more memory is required (although you can call gc_collect_cycles to force garbage collection at any time).

Of course all of this does not come into consideration in the typical case of "service a request and then exit".

like image 107
Jon Avatar answered Sep 23 '22 16:09

Jon


Normally this is not an issue you have to take into account. Here is an article about one guy who met this issue.

http://paul-m-jones.com/archives/262

you can use this function to destruct whatever object you like within your classes.

function __destruct()
{
    //do stuff
}

An object can remain in the memory for the duration of the request, or when invoking from command line, as long as the script remains running.

like image 38
furier Avatar answered Sep 25 '22 16:09

furier