1

I'm getting started with objects in PHP and I was wondering when they get deleted. Is it when the PHP file gets finished loading or when it's finished with the function I'm calling? is there anyway to keep the object alive so it can be called in another instance when the file is loaded.

Andre Yonadam
  • 964
  • 1
  • 13
  • 30
  • 3
    There's nothing quite like [reading the manual](http://php.net/manual/en/features.gc.php), which also happens to be the top hit for "PHP garbage collection" on A Famous Search Engine. :-) – T.J. Crowder Oct 12 '13 at 16:52
  • 3
    Your second question (*"is there anyway to keep the object alive so it can be called in another instance when the file is loaded"*) is really quite different from your first. If the second question is your *real* goal, I recommend rewording to make that the main focus. – T.J. Crowder Oct 12 '13 at 16:54
  • 1
    Where do PHP objects go when they die? They don't go to heaven where the angels fly, Go to a lake of fire and fry, See them again 'till the Fourth of July – AbraCadaver Oct 12 '13 at 17:03
  • @T.J.Crowder The OP may not have known that "garbage collection" was the key phrase to look for, and nor should they need to, since PHP is not primarily a garbage collected runtime. That section of the manual does describe what the runtime primarily does, though, which is reference counting. – IMSoP Oct 13 '13 at 02:43
  • 1
    @IMSoP: Reference counting is a *form* of garbage collection. (And agreed that the OP may not have known the term; that's why I gave the link. But as I had nothing to add, it wasn't an *answer*.) – T.J. Crowder Oct 13 '13 at 09:18
  • Thanks for the help guys. I heard of garbage collection before but didn't think of it when I came across this question. Now that I understand things better, I should have split the two questions up. – Andre Yonadam Oct 13 '13 at 14:54
  • 1
    @T.J.Crowder Yeah, I've always thought of "garbage collection" meaning specifically the act of a separate "Garbage Collector" that gets periodically invoked, which I realise now isn't quite right. PHP has one of those too, but most of its garbage collection is done without a garbage collector... :| – IMSoP Oct 13 '13 at 17:33
  • @IMSoP: :-) Well put! – T.J. Crowder Oct 13 '13 at 18:02

2 Answers2

3

There's a distinction to be made between objects dying and objects being out of scope.

If you are concerned with the logistics of memory management and garbage collection in PHP then as T.J. Crowder pointed out you could read the manual on garbage collection in PHP.

If on the other hand you are more concerned with variable scope, then the answer is that the scope of variables is typically bounded to the block they are declared in. But you can also create global variables, and access them using the global keyword inside of functions - although global variables are generally a bad idea. See the manual for details.

And as far as persisting variables beyond a script, this can only be accomplished via some sort of storage mechanism. In the context of web applications, that is usually accomplished using session state but be careful that the nuances of persisting objects from one session to the next (i.e. one invocation of a script to the next) may be different depending on whether the session state is stored in-process, or out of process. In case it's the latter then the objects will be serialized and deserialzed which makes things a little more complicated.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
-2

PHP memory vars is garbage collected, usually they are being removed (decrease ref) when request ends, function out of scope .. etc. you still can go with singleton pattern, and load only not loaded objects note that this only works for each single request, if you want to keep the object in memory for more than one request, that's wont work for php,

/**
 * Singleton class
 *
 */
final class UserFactory
{
   /**
    * Call this method to get singleton
    *
    * @return UserFactory
    */
  public static function Instance()
  {
    static $inst = null;
    if ($inst === null) {
        $inst = new UserFactory();
    }
    return $inst;
}

/**
 * Private ctor so nobody else can instance it
 *
 */
private function __construct()
{

}
}

To use:

$fact = UserFactory::Instance();
$fact2 = UserFactory::Instance();
$fact == $fact2;

code example taken from https://stackoverflow.com/a/203359/1291995

Community
  • 1
  • 1
Omiga
  • 572
  • 3
  • 11
  • Reference counting `!=` garbage collection. For most purposes, a PHP object is destructed and released from memory *as soon as its reference count reaches zero*. The separate "garbage collector" only kicks in for circular references, which can never reach a reference count of zero. – IMSoP Oct 13 '13 at 02:41
  • 1. i didn't say reference count == gc. 2. that's what i said, released when ref goes to 0. I don't see what is your objection here – Omiga Oct 13 '13 at 03:03
  • Your answer begins with the phrase "PHP memory vars is garbage collected". I guess the reference counting is a form of garbage collection in one sense, but they're not usually used interchangeably, particularly since PHP has both for different purposes. – IMSoP Oct 13 '13 at 03:19
  • I didn't know that php uses reference counting for other purposes! What it's used for rather than in GC ? – Omiga Oct 13 '13 at 03:32
  • Sorry, my sentence wasn't very clear: the term "garbage collection" is often used to mean the action of a specific "garbage collector", as opposed to other "automatic memory management" such as reference counting. In PHP, a Garbage Collector exists, but is a supplement to the older refcount system, although I now realise both systems could be called "garbage collection". – IMSoP Oct 13 '13 at 04:12