Ver objetos de una clase en php

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?

preguntado el 27 de agosto de 11 a las 19:08

4 Respuestas

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.

Respondido 27 ago 11, 23:08

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.

Respondido 27 ago 11, 23:08

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.

Respondido 27 ago 11, 23:08

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 ? - SA Malik

I cannot elaborate further because I'm not familiar with WP. - Michael Berkowski

¿Ayudará esto?

<?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';

Respondido 28 ago 11, 00:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.