# make new return a reference to existent object

- Thread: https://externals.io/message/16179
- Original archive: https://news-web.php.net/php.internals/16179
- Started by Leonardo Pedretti on 2005-05-09T14:30:56+00:00
- Messages: 12
- 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

---

## #16179 — Leonardo Pedretti — 2005-05-09T14:30:56+00:00

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

I would like (for code cleanliness purposes) to make 'new' return a reference 
to an already created object under certain circumstances without using a 
factory, is it possible?

Thanx
-- 

Leonardo Pedretti
Axon Sistemas
Líder de Equipo Proyecto Basalto

---

## #16180 — Jason Barnett — 2005-05-09T15:45:17+00:00

URL: https://externals.io/message/16179#16180 · In reply to: #16179

Leonardo Pedretti wrote:
> I would like (for code cleanliness purposes) to make 'new' return a reference 
> to an already created object under certain circumstances without using a 
> factory, is it possible?
> 
> Thanx

"Under certain circumatances" ? ...

If it's only under certain circumstances that you want a reference 
versus a copy versus an actual new object... well then why not use 
factory methods?

Or perhaps you meant something like a "Singleton":
http://phppatterns.com/index.php/article/articleview/6/1/1/

---

## #16183 — Sara Golemon — 2005-05-09T18:12:13+00:00

URL: https://externals.io/message/16179#16183 · In reply to: #16179

> I would like (for code cleanliness purposes) to make 'new'
> return a reference to an already created object under certain
> circumstances without using a factory, is it possible?
>
* Sara whispers "old" into the conversation and runs away

---

## #16184 — Jason Sweat — 2005-05-09T19:13:32+00:00

URL: https://externals.io/message/16179#16184 · In reply to: #16183

> > […]
> * Sara whispers "old" into the conversation and runs away

It's not "old", it is an experienced object ;)

Regards,
Jason
http://blog.casey-sweat.us/

---

## #16185 — Jason Garber — 2005-05-09T19:35:52+00:00

URL: https://externals.io/message/16179#16185 · In reply to: #16179

Hello,

  -1

  New implies "new".  Use a factory.

  Maybe we should add an old construct:

     $object = old MyClass();

  Just kidding :)
  
-- 
Best regards,
 Jason                            mailto:jason@ionzoft.com

Monday, May 9, 2005, 10:30:56 AM, you wrote:

LP> I would like (for code cleanliness purposes) to make 'new' return a reference
LP> to an already created object under certain circumstances without using a
LP> factory, is it possible?

LP> Thanx

---

## #16197 — Leonardo Pedretti — 2005-05-10T13:46:19+00:00

URL: https://externals.io/message/16179#16197

It would not be such a problem to write something that does the same task 
through a static or global function, but unfortunately it is impossible, due 
to the following: you can build a cache in an array, then in a __new() 
function you could check if the requested object is already loaded and return 
a reference. However, if this is the case, objects that enter the cache never 
leave it because they are still referenced in the array when all other 
references have gone, so they never destruct, except when the array is 
destroyed at program's end.
So, the 'cache' would grow to include all possible objects, that's not good.
Now, the question is not if there should be an 'existing' keyword for 
replacing singletons... singletons are ok, but there may be some 
circumstances when one would like to return some already existing instance of 
some class (between other existing instances, determined by the $id member 
for example) and get the destructor executed when all references to that 
instance are gone. However, it is not possible to do it because there is not 
any way to detect the removal of a reference to an object instance, except 
implementing specific (and really ugly) methods for assigning and deassigning 
the variables, such as __new(&$var) and __release(&$var) functions that do 
the task of incrementing and decrementing a reference count for the object 
and remove it from the cache when the refcount reaches 0. The main 
disadvantage of this is that any request for an object of the mentioned class 
should be matched with a __release() function much like in a C++ program 
using new and delete.
Is there any way of checking the existing instances of a class and returning 
the same instance in a function without implementing that cache system, so 
when all references are gone it naturally gets destroyed by the normal and 
natural php code flux?

Leo



> I think Leonardo might have a point here. Built-in support for
> Singletons could be a nice feature, without the use of static
> functions, would be nice. I wouldn't use the 'new' keyword however. I
> think something along the lines of existing, or ref
>
> $ref = existing TEST; // which would return an existing instance of
> TEST, if one exists, and a new one if it doesn't. ( pretty much like
> the singleton code, except no need for singleton code)
>
> I don't think it's anything important, and there are more pressing
> matters, to be worked on, I'm sure, but if someone created the code,
> and it was secure / bug-free / compliant-with-coding-standards / etc,
> I can't see why it couldn't(or shouldn't) be included. When all's said
> and done though, it's still just syntactic sugar. It all depends on
> how sweet the devs consider it.
>
> 
> > […]

-- 

Leonardo Pedretti
Axon Sistemas
Líder de Equipo Proyecto Basalto

---

## #16198 — Lukas Smith — 2005-05-10T14:26:03+00:00

URL: https://externals.io/message/16179#16198 · In reply to: #16197

Leonardo Pedretti wrote:
> It would not be such a problem to write something that does the same task 
> through a static or global function, but unfortunately it is impossible, due 
> to the following: you can build a cache in an array, then in a __new() 
> function you could check if the requested object is already loaded and return 
> a reference. However, if this is the case, objects that enter the cache never 
> leave it because they are still referenced in the array when all other 
> references have gone, so they never destruct, except when the array is 
> destroyed at program's end.

PHP5 has destructors ...

regards,
Lukas

---

## #16199 — Leonardo Pedretti — 2005-05-10T14:48:56+00:00

URL: https://externals.io/message/16179#16199 · In reply to: #16198

Yes, the question was pointed in another way, the destructors are not called 
until the last reference to an object has been removed, but if you store a 
reference in an array so you lately return a reference to the already loaded 
instance instead of creating an aliasing of that instance, then you get the 
undesirable behavior that the object is never destroyed until the script 
ends, because there is always a reference in that array and you can not check 
when references are removed in any way.
And leaving the array growing while each object that exists in a huge database 
is slowly loaded and never unloaded, is not an option.

Leo


> Leonardo Pedretti wrote:
> > […]
>
> PHP5 has destructors ...
>
> regards,
> Lukas

-- 

Leonardo Pedretti
Axon Sistemas
Líder de Equipo Proyecto Basalto

---

## #16201 — Lukas Smith — 2005-05-10T14:57:37+00:00

URL: https://externals.io/message/16179#16201 · In reply to: #16199

Leonardo Pedretti wrote:
> Yes, the question was pointed in another way, the destructors are not called 
> until the last reference to an object has been removed, but if you store a 
> reference in an array so you lately return a reference to the already loaded 
> instance instead of creating an aliasing of that instance, then you get the 
> undesirable behavior that the object is never destroyed until the script 
> ends, because there is always a reference in that array and you can not check 
> when references are removed in any way.
> And leaving the array growing while each object that exists in a huge database 
> is slowly loaded and never unloaded, is not an option.

then dont store a reference to the object but instead just store the key 
in the array.

regards,
Lukas

---

## #16202 — Leonardo Pedretti — 2005-05-10T15:16:14+00:00

URL: https://externals.io/message/16179#16202 · In reply to: #16201

How do i obtain the object again having just the key when i want to return it 
in another call, if i don't have a reference somewhere?
The point would be: object asked id #1, it is not already loaded, load it and 
return it, end. Then, in another call: object asked id #1, it is already 
loaded, return the same instance already in memory, end. And outside of this 
function, if all references of that object are deleted, then the object is 
unloaded from memory and so destructed.
The behavior i want to reach is that if an instance of some specific class has 
the same $id that another instance, then they are the same instance.
It's for a database persistent object framework.
Everyone that tries to program such a framework gets to the same problem.

Regards
Leo


> Leonardo Pedretti wrote:
> > […]
>
> then dont store a reference to the object but instead just store the key
> in the array.
>
> regards,
> Lukas

-- 

Leonardo Pedretti
Axon Sistemas
Líder de Equipo Proyecto Basalto

---

## #16204 — Wez Furlong — 2005-05-10T16:10:51+00:00

URL: https://externals.io/message/16179#16204 · In reply to: #16199

Chicken and egg; you can't keep the object alive without a reference.
There is no way to achieve what you want, and we're not going to
change the behaviour of the new operator, nor are we going to build
some kind of singleton factory thingy into the core language; you have
the tools to do it "the right way", please use them and kill these two
threads.

--Wez.


> Yes, the question was pointed in another way, the destructors are not called
> until the last reference to an object has been removed, but if you store a
> reference in an array so you lately return a reference to the already loaded
> instance instead of creating an aliasing of that instance, then you get the
> undesirable behavior that the object is never destroyed until the script
> ends, because there is always a reference in that array and you can not check
> when references are removed in any way.
> And leaving the array growing while each object that exists in a huge database
> is slowly loaded and never unloaded, is not an option.
> 
> Leo
> 
> 
> > […]
> 
> --
> 
> Leonardo Pedretti
> Axon Sistemas
> Líder de Equipo Proyecto Basalto

---

## #16206 — Leonardo Pedretti — 2005-05-10T20:18:39+00:00

URL: https://externals.io/message/16179#16206 · In reply to: #16204

Not so chicken and egg if, through reflection, a ReflectionClass object could 
inform all the instances of that class in a simple array or with an iterator.
May be there's another even better place where to implement such a 
functionality, i'm just pointing reflection because i think it's natural that 
a class should know what instances of itself are around there.

Regards
Leo


> Chicken and egg; you can't keep the object alive without a reference.
> There is no way to achieve what you want, and we're not going to
> change the behaviour of the new operator, nor are we going to build
> some kind of singleton factory thingy into the core language; you have
> the tools to do it "the right way", please use them and kill these two
> threads.
>
> --Wez.
>
> 
> > […]

-- 

Leonardo Pedretti
Axon Sistemas
Líder de Equipo Proyecto Basalto
