# [Q] references and objects

- Thread: https://externals.io/message/54295
- Original archive: https://news-web.php.net/php.internals/54295
- Started by Flavius Aspra on 2011-08-02T20:05:38+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #54295 — Flavius Aspra — 2011-08-02T20:05:38+00:00

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

I have a zval *obj holding an object of class B, which I then add to an 
array member of another object of class A. Then I create a new instance 
*obj2 of class B, to which I want to add a reference to *obj. How would 
I best go about it?

So A works as a "pool" of B's, and B's may reference to each other.

Please tell me key functions and macros to use to accomplish 
specifically this thing, as the API is quite new to me.

I have thought about setting Z_SET_ISREF_P() manually, but what else 
would I need? From my understanding, I'd still need a new zval* for 
that. Any other better ways?

Regards,
Flavius

---

## #54296 — Etienne Kneuss — 2011-08-02T21:03:30+00:00

URL: https://externals.io/message/54295#54296 · In reply to: #54295

Hello,


> I have a zval *obj holding an object of class B, which I then add to an
> array member of another object of class A. Then I create a new instance
> *obj2 of class B, to which I want to add a reference to *obj. How would I
> best go about it?
>
> So A works as a "pool" of B's, and B's may reference to each other.
>
> Please tell me key functions and macros to use to accomplish specifically
> this thing, as the API is quite new to me.
>
> I have thought about setting Z_SET_ISREF_P() manually, but what else would I
> need? From my understanding, I'd still need a new zval* for that. Any other
> better ways?

You generally don't want to use references.

Rather, you can keep the same zval* pointer as long as you
increment/decrement its refcount (Z_ADDREF_P/zval_ptr_dtor) so that it
correctly keeps track of how many pointers are pointing to it.

Unless I misunderstood what you're trying to do, it's that easy.

Best,

>
> Regards,
> Flavius
>
> --
> 
> 
>
>



-- 
Etienne Kneuss
http://www.colder.ch
