简体   繁体   中英

See Objects of a class in 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?

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM