0

How can i see How many objects of a class are loaded in php. Also do the objects get loaded in a single session on server? Or one can track objects from other sessions also while on the server side?

Actually i am confused. When an object is loaded with the PHP where does it reside? Is it in the browser? Is it in the session and expires as soon as the session expire?

S. A. Malik
  • 3,465
  • 6
  • 37
  • 56
  • possible duplicate of [Listing all objects of a certain class](http://stackoverflow.com/questions/2172959/listing-all-objects-of-a-certain-class) or [Get all objects of a particular class](http://stackoverflow.com/questions/6492333/get-all-objects-of-a-particular-class) and [Get all instances of a class in PHP](http://stackoverflow.com/questions/475569/get-all-instances-of-a-class-in-php) – mario Aug 27 '11 at 20:04

4 Answers4

0

How can i see How many objects of a class are loaded in php.

I don't think there is a way to do this without you actually keeping count in the class's constructor.

When an object is loaded with the PHP where does it reside? Is it in the browser? Is it in the session and expires as soon as the session expire?

It resides inside the memory that the PHP process that gets called for that one request allocates. It expires as soon as the current request has finished or been terminated (or been unset()).

The session is something that helps identify a user across multiple requests. It survives longer - it expires when it gets destroyed, when the user's session cookie is deleted, or when the session reaches its expiry time.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

An object is just a complex variable. It can hold a couple of simple types together and it can have functions.

Despite the numerous differences between simple types and objects, an objects is just a variable. Objects are not shared over sessions, or sent to browsers any more than simple integers or strings.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

An object exists only on the server, in memory, and only for the lifetime of the script's execution unless saved into the user's $_SESSION. Even when saved, it ceases to be an object and instead becomes a serialized string. It can be reconstituted again into an object in the same session or a later session.

The script's lifetime refers to the moment the web server calls it until the moment the scripts final line has been processed. The PHP engine may dispose of objects no longer needed by the script through garbage collection, even before the script has fully terminated.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • OK! But i was going through wordpress code and while playing it i came to know that there exists an object named wp_user even when there is a script is running that has nothing to do with wp_user. Can you elaborate on it ? – S. A. Malik Sep 05 '11 at 19:41
  • I cannot elaborate further because I'm not familiar with WP. – Michael Berkowski Sep 06 '11 at 02:38
0

Will this help?

<?php

class Hello {
    public function __construct() {
    }
}

$hello = new Hello;
$hi = new Hello;

$i = 0;

foreach (get_defined_vars() as $key => $value) {
    if (is_object($value) && get_class($value) == 'Hello')
        $i++;
}

echo 'There are ' . $i . ' instances of class Hello';
Vladimir
  • 818
  • 5
  • 13