FAQ
I see little value in having a function that tells me how many objects
of any kind I have instantiated. Mostly, I see it as a way to help
teach people about assignment of objects works in PHP:

$a = new MyClass();
$b = $a;
var_dump(get_object_count());

And that's about the sum of how useful I think this is.

If it were to be able to narrow it to a specific class then it *might*
be a little more useful.

Search Discussions

  • Madara Uchiha at Apr 8, 2013 at 8:07 pm
    I'm with Morrison, I see no actual use for this.

    It's "cool", but what would you use it for?
    On Mon, Apr 8, 2013 at 10:47 PM, Levi Morrison wrote:
    I see little value in having a function that tells me how many objects
    of any kind I have instantiated. Mostly, I see it as a way to help
    teach people about assignment of objects works in PHP:

    $a = new MyClass();
    $b = $a;
    var_dump(get_object_count());

    And that's about the sum of how useful I think this is.

    If it were to be able to narrow it to a specific class then it *might*
    be a little more useful.

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
  • Sherif Ramadan at Apr 8, 2013 at 8:25 pm

    On Mon, Apr 8, 2013 at 4:07 PM, Madara Uchiha wrote:

    I'm with Morrison, I see no actual use for this.

    It's "cool", but what would you use it for?
    On Mon, Apr 8, 2013 at 10:47 PM, Levi Morrison wrote:
    I see little value in having a function that tells me how many objects
    of any kind I have instantiated. Mostly, I see it as a way to help
    teach people about assignment of objects works in PHP:

    $a = new MyClass();
    $b = $a;
    var_dump(get_object_count());

    And that's about the sum of how useful I think this is.

    If it were to be able to narrow it to a specific class then it *might*
    be a little more useful.
    It would be useful if you had functions in PHP that could tell you where
    instances are stored in the current scope. Extensions like Reflection are
    currently used in a wide variety of ways to produce autonomous factory
    builders that can workout the relationships between different classes and
    virtually induce dependency injection. But PHP currently has no way to
    figure out where instances are sitting around in memory since the engine
    abstracts this nuisance of memory management away from user space (and for
    good reason).

    As an example if you had the following code, it would not be obvious why
    the object is not destroyed at the point unset($object) is called if you
    were new to PHP.

    $object = new stdClass;
    $foo = $object;
    unset($object);

    A lot of people depend on destructors to solve cleanup problems in complex
    PHP code bases. Though PHP won't invoke a destructor until the last
    instance to an object has been destroyed, but it's not always obvious where
    an instance sits unless you work this out ahead of time in your code.


    /* For example building on the previous code... */
    $object = new stdClass;
    $foo = $object;

    /* If you knew what variables carry the instance you could easily see it's
    the same instance */
    var_dump($object, $foo);
    object(stdClass)#1 (0) {
    }
    object(stdClass)#1 (0) {
    }


    But there's no easy way to work out these relationships in user space
    without writing a lot of boilerplate code. You would have to hack a
    combination of get_defined_vars() and var_dump() with output buffering to
    basically manually parse out class names and instance numbers then work out
    the relationships yourself. That would be pretty slow in user space and
    cumbersome.


    /* If we had a function that could give you the variable to class/instance
    relationship directly in each scope that could be quite useful */
    var_dump(get_class_instances());

    array(2) {
    ["object"]=>
    array(2) {
    ["class"]=>
    string(8) "stdClass"
    ["instance"]=>
    int(1)
    }
    ["foo"]=>
    array(2) {
    ["class"]=>
    string(8) "stdClass"
    ["instance"]=>
    int(1)
    }
    }


    So assuming the above example is an array of variable/class relationships
    we could more easily workout that $foo and $object are pointing to the same
    instance. I don't think it's such a strongly needed feature though. I just
    think this in combination with the same problem we have with references
    (you can't find references autonomously either) would be neat. That's all.

    </my two cents>


    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
  • Joe Watkins at Apr 9, 2013 at 12:29 am

    On 04/08/2013 09:07 PM, Madara Uchiha wrote:
    I'm with Morrison, I see no actual use for this.

    It's "cool", but what would you use it for?
    On Mon, Apr 8, 2013 at 10:47 PM, Levi Morrison wrote:
    I see little value in having a function that tells me how many objects
    of any kind I have instantiated. Mostly, I see it as a way to help
    teach people about assignment of objects works in PHP:

    $a = new MyClass();
    $b = $a;
    var_dump(get_object_count());

    And that's about the sum of how useful I think this is.

    If it were to be able to narrow it to a specific class then it *might*
    be a little more useful.

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
    I see some merit in the idea that during development of an application
    it could be useful to get a profile view of the object store, without
    affecting those objects or introducing new objects in order to provide
    that view.

    There are other run-time use cases, but for me, mostly development and
    debugging, without being intrusive or having any real impact on the
    application or object store.
  • David Muir at Apr 9, 2013 at 12:53 am

    On 09/04/13 10:29, Joe Watkins wrote:
    On 04/08/2013 09:07 PM, Madara Uchiha wrote:
    I'm with Morrison, I see no actual use for this.

    It's "cool", but what would you use it for?

    On Mon, Apr 8, 2013 at 10:47 PM, Levi Morrison
    wrote:
    I see little value in having a function that tells me how many objects
    of any kind I have instantiated. Mostly, I see it as a way to help
    teach people about assignment of objects works in PHP:

    $a = new MyClass();
    $b = $a;
    var_dump(get_object_count());

    And that's about the sum of how useful I think this is.

    If it were to be able to narrow it to a specific class then it *might*
    be a little more useful.

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
    I see some merit in the idea that during development of an application
    it could be useful to get a profile view of the object store, without
    affecting those objects or introducing new objects in order to provide
    that view.

    There are other run-time use cases, but for me, mostly development and
    debugging, without being intrusive or having any real impact on the
    application or object store.
    Isn't that what a debugger is for?

    Cheers,
    David
  • Joe Watkins at Apr 9, 2013 at 8:04 am

    On 04/09/2013 01:53 AM, David Muir wrote:
    On 09/04/13 10:29, Joe Watkins wrote:
    On 04/08/2013 09:07 PM, Madara Uchiha wrote:
    I'm with Morrison, I see no actual use for this.

    It's "cool", but what would you use it for?

    On Mon, Apr 8, 2013 at 10:47 PM, Levi Morrison
    wrote:
    I see little value in having a function that tells me how many objects
    of any kind I have instantiated. Mostly, I see it as a way to help
    teach people about assignment of objects works in PHP:

    $a = new MyClass();
    $b = $a;
    var_dump(get_object_count());

    And that's about the sum of how useful I think this is.

    If it were to be able to narrow it to a specific class then it *might*
    be a little more useful.

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
    I see some merit in the idea that during development of an application
    it could be useful to get a profile view of the object store, without
    affecting those objects or introducing new objects in order to provide
    that view.

    There are other run-time use cases, but for me, mostly development and
    debugging, without being intrusive or having any real impact on the
    application or object store.
    Isn't that what a debugger is for?

    Cheers,
    David
    No, that's shooting at an ant with a bazooka ...
  • Laruence at Apr 9, 2013 at 10:24 am
    if a class need that, it can implement a interface like instance_counter,
    which will simply can implement like:

    class A {
    public static $instance_counter = 0;

    public function __construct() { self::$instance_counter++; }

    public function __destruct() { self::$instance_counter--; };

    public static function get_counter() { return self::$instance_counter; }
    }

    thanks

    On Tue, Apr 9, 2013 at 6:18 PM, Frank Liepert wrote:

    Hello internals,

    I updated the RFC (https://wiki.php.net/rfc/instance_counter):

    - added support for a class name, so the function can be narrowed down to a
    specific class
    - added use case


    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php

    --
    Laruence Xinchen Hui
    http://www.laruence.com/
  • Joe Watkins at Apr 9, 2013 at 10:30 am

    On 04/09/2013 11:23 AM, Laruence wrote:
    if a class need that, it can implement a interface like instance_counter,
    which will simply can implement like:

    class A {
    public static $instance_counter = 0;

    public function __construct() { self::$instance_counter++; }

    public function __destruct() { self::$instance_counter--; };

    public static function get_counter() { return self::$instance_counter; }
    }

    thanks

    On Tue, Apr 9, 2013 at 6:18 PM, Frank Liepert wrote:

    Hello internals,

    I updated the RFC (https://wiki.php.net/rfc/instance_counter):

    - added support for a class name, so the function can be narrowed down to a
    specific class
    - added use case


    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
    As mentioned in the RFC, that's an ugly and very intrusive solution,
    that is not always practical (you wouldn't want to wrap every internal
    class you wish to see the count of).

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedApr 8, '13 at 7:47p
activeApr 9, '13 at 10:30a
posts8
users6
websitephp.net

People

Translate

site design / logo © 2023 Grokbase