# new Class ReflectionZVal

- Thread: https://externals.io/message/64640
- Original archive: https://news-web.php.net/php.internals/64640
- Started by nathan@starin.biz on 2013-01-07T17:10:27+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #64640 — nathan@starin.biz — 2013-01-07T17:10:27+00:00

URL: https://externals.io/message/64640#64640

I wanted some thought from you guys about the idea of giving PHP scripts the
ability to read some internal information about variables such as ZVals.

 

The idea of how this reflection class would work would be something like:

 

Class ReflectionZVal {

    // Retains info about the variable referenced

    function __construct(&$variable);

    // Returns if the referenced variable

    function valueExists();

    // Returns a ref to the variable or throws error if value is gone.

    function &getValue();

    // Returns the reference count of the variable

    function getRefCount();

    // Returns the current type associated with the referenced variable

    function `getType()`;

    // Returns bool of if getValueReflection() is callable. (see note below)

    function isValueReflectable();

    // Returns a new instance of ReflectionZVal for the value of variable
passed in construct. (returns null and throws error if value is not a ref of
a variable)

    function getValueReflection();

}

 

The idea of this class would give PHP the ability to read reference count
info and have read-only access to values stored in variables without causing
PHP to not be able to use the internal garbage collector. An example of how
this could be useful would be if you had a class structure that matched a
database table structure. Every time a new record of the table was returned
it constructed a new object. It is more practical to have the class store
each object that was constructed into a static variable attached to each
class structure and when a new record is requested it first checks to see if
the object with the same row has already been constructed and use/update
that object instead of making (possibly) many duplicates. This gives the
code structure the ability that if the same record was requested then later
in the execution the same row was requested again and modified it would also
modify it everywhere that object/row was already requested. The issue with
this structure is that php's garbage collector will never clean up those
constructed objects because the class needs a reference to them and cannot
know if other places in the code has references to them.

 

If a reflection like this one was available it would give the ability to
contain a reference to variables/pointers without increasing the reference
count. It would allow a class structure (like stated above) to call it's own
garbage collector every once in a while to clean up those ReflectionZVal
objects by checking the result of ReflectionZVal:: valueExists ().

 

Is there anything obvious (limitation wise) that I am missing or is this
RFC'able?

 

Software Developer

Nathan Bruer

---

## #64641 — Anthony Ferrara — 2013-01-07T17:15:31+00:00

URL: https://externals.io/message/64640#64641 · In reply to: #64640

Nathan,



> I wanted some thought from you guys about the idea of giving PHP scripts
> the
> ability to read some internal information about variables such as ZVals.
>
>
>
> The idea of how this reflection class would work would be something like:
>
>
>
> Class ReflectionZVal {
>
>     // Retains info about the variable referenced
>
>     function __construct(&$variable);
>
>     // Returns if the referenced variable
>
>     function valueExists();
>
>     // Returns a ref to the variable or throws error if value is gone.
>
>     function &getValue();
>
>     // Returns the reference count of the variable
>
>     function getRefCount();
>
>     // Returns the current type associated with the referenced variable
>
>     function `getType()`;
>
>     // Returns bool of if getValueReflection() is callable. (see note
> below)
>
>     function isValueReflectable();
>
>     // Returns a new instance of ReflectionZVal for the value of variable
> passed in construct. (returns null and throws error if value is not a ref
> of
> a variable)
>
>     function getValueReflection();
>
> }
>
>
>
> The idea of this class would give PHP the ability to read reference count
> info and have read-only access to values stored in variables without
> causing
> PHP to not be able to use the internal garbage collector. An example of how
> this could be useful would be if you had a class structure that matched a
> database table structure. Every time a new record of the table was returned
> it constructed a new object. It is more practical to have the class store
> each object that was constructed into a static variable attached to each
> class structure and when a new record is requested it first checks to see
> if
> the object with the same row has already been constructed and use/update
> that object instead of making (possibly) many duplicates. This gives the
> code structure the ability that if the same record was requested then later
> in the execution the same row was requested again and modified it would
> also
> modify it everywhere that object/row was already requested. The issue with
> this structure is that php's garbage collector will never clean up those
> constructed objects because the class needs a reference to them and cannot
> know if other places in the code has references to them.
>
>
>
> If a reflection like this one was available it would give the ability to
> contain a reference to variables/pointers without increasing the reference
> count. It would allow a class structure (like stated above) to call it's
> own
> garbage collector every once in a while to clean up those ReflectionZVal
> objects by checking the result of ReflectionZVal:: valueExists ().
>
>
>
> Is there anything obvious (limitation wise) that I am missing or is this
> RFC'able?
>

This is an AWESOME idea.

The only question that I'd have is whether it should be implemented in
reflection or as a language construct. The former would have issues with
pass-by-reference and reference counting (debugging a variable would
actually cause a separation and make refcount useless for non-referenced
variables)... The latter would require parser changes, but eliminate that
issue (as it could access the compiled variable directly)... Then again,
that's dirty as it requires new keywords in the language...

But I love the concept...

Anthony

---

## #64642 — Nikita Popov — 2013-01-07T17:18:20+00:00

URL: https://externals.io/message/64640#64642 · In reply to: #64640

> I wanted some thought from you guys about the idea of giving PHP scripts
> the
> ability to read some internal information about variables such as ZVals.
>
>
>
> The idea of how this reflection class would work would be something like:
>
>
>
> Class ReflectionZVal {
>
>     // Retains info about the variable referenced
>
>     function __construct(&$variable);
>
>     // Returns if the referenced variable
>
>     function valueExists();
>
>     // Returns a ref to the variable or throws error if value is gone.
>
>     function &getValue();
>
>     // Returns the reference count of the variable
>
>     function getRefCount();
>
>     // Returns the current type associated with the referenced variable
>
>     function `getType()`;
>
>     // Returns bool of if getValueReflection() is callable. (see note
> below)
>
>     function isValueReflectable();
>
>     // Returns a new instance of ReflectionZVal for the value of variable
> passed in construct. (returns null and throws error if value is not a ref
> of
> a variable)
>
>     function getValueReflection();
>
> }
>
>
>
> The idea of this class would give PHP the ability to read reference count
> info and have read-only access to values stored in variables without
> causing
> PHP to not be able to use the internal garbage collector. An example of how
> this could be useful would be if you had a class structure that matched a
> database table structure. Every time a new record of the table was returned
> it constructed a new object. It is more practical to have the class store
> each object that was constructed into a static variable attached to each
> class structure and when a new record is requested it first checks to see
> if
> the object with the same row has already been constructed and use/update
> that object instead of making (possibly) many duplicates. This gives the
> code structure the ability that if the same record was requested then later
> in the execution the same row was requested again and modified it would
> also
> modify it everywhere that object/row was already requested. The issue with
> this structure is that php's garbage collector will never clean up those
> constructed objects because the class needs a reference to them and cannot
> know if other places in the code has references to them.
>
>
>
> If a reflection like this one was available it would give the ability to
> contain a reference to variables/pointers without increasing the reference
> count. It would allow a class structure (like stated above) to call it's
> own
> garbage collector every once in a while to clean up those ReflectionZVal
> objects by checking the result of ReflectionZVal:: valueExists ().
>
>
>
> Is there anything obvious (limitation wise) that I am missing or is this
> RFC'able?
>
>
>
> Software Developer
>
> Nathan Bruer
>

I think what you are looking for here is a weak ref. See
http://pecl.php.net/package/Weakref

Nikita

---

## #64643 — Derick Rethans — 2013-01-07T17:22:14+00:00

URL: https://externals.io/message/64640#64643 · In reply to: #64640

> I wanted some thought from you guys about the idea of giving PHP scripts the
> ability to read some internal information about variables such as ZVals.
> 
>  
> 
> The idea of how this reflection class would work would be something like:
> 
>  
> 
> Class ReflectionZVal {
> 
>     // Retains info about the variable referenced
> 
>     function __construct(&$variable);

That's inherently flawed, as passing a variable in can modify it's 
refcount and is_ref states. This is why Xdebug's equivalent wants a 
string with the variable name instead:

http://xdebug.org/docs/all_functions#xdebug_debug_zval
 
>     // Returns if the referenced variable
> 
>     function valueExists();

What's a "referenced variable" here? If you pass in something, it will 
exist.

cheers,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug
Posted with an email client that doesn't mangle email: alpine

---

## #64645 — nathan@starin.biz — 2013-01-07T18:34:45+00:00

URL: https://externals.io/message/64640#64645 · In reply to: #64643

From: Derick Rethans [mailto:derick@php.net] 
Sent: Monday, January 07, 2013 11:22 AM
To: nathan@starin.biz
Cc: internals@lists.php.net
Subject: Re: [PHP-DEV] new Class ReflectionZVal



> > […]
> That's inherently flawed, as passing a variable in can modify it's
refcount and is_ref states. This is why Xdebug's equivalent wants a string
with the variable name instead:
> http://xdebug.org/docs/all_functions#xdebug_debug_zval

I am quite familiar with this issue. The idea I was thinking to solve that
issue is to maybe pass by-reference to the __construct. When I was drafting
this first email I had it by-ref, but opted out of it because I didn't have
a way to get the value of the reference, but reviewing I added the "
function getValueReflection();" functions which should make it possible to
use by-reference now and obtain the info to the reference of the reference.

> > […]
> What's a "referenced variable" here? If you pass in something, it will
exist.

The idea is that the value may not exist. This function would confirm that
the value of the reflected zval still exists. My first draft of this email
only had "function &getValue();" but I think it would create problems if it
stated something like, "Returns null if value does not exist" because what
if the value does exist (ie variable reference is still alive) but the value
is null? This function (ReflectionZVal::valueExists()) would return false if
the zval is gone (garbage collected).

---

## #64647 — Derick Rethans — 2013-01-07T19:07:22+00:00

URL: https://externals.io/message/64640#64647 · In reply to: #64645

> From: Derick Rethans [mailto:derick@php.net] 
> Sent: Monday, January 07, 2013 11:22 AM
> 
> > […]
> 
> I am quite familiar with this issue. The idea I was thinking to solve that
> issue is to maybe pass by-reference to the __construct.

It won't, as you might actually end up splitting the reference. You 
*cannot* pass in a variable, either by-ref or by-value for this to work.

> When I was drafting this first email I had it by-ref, but opted out of 
> it because I didn't have a way to get the value of the reference, but 
> reviewing I added the " function getValueReflection();" functions 
> which should make it possible to use by-reference now and obtain the 
> info to the reference of the reference.

There is no such thing as a reference of a reference. 

cheers,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug
Posted with an email client that doesn't mangle email: alpine
