On Mon, Apr 8, 2013 at 4:07 PM, Madara Uchiha <dor.tchi...@gmail.com> 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 <morrison.l...@gmail.com>
> 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
>
>

Reply via email to