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;

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;

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

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

array(2) {
array(2) {
string(8) "stdClass"
array(2) {
string(8) "stdClass"

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

Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 8 | next ›
Discussion Overview
groupphp-internals @
postedApr 8, '13 at 7:47p
activeApr 9, '13 at 10:30a



site design / logo © 2022 Grokbase