I came across something strange the other day in php, but this question is more general than that.
What I wonder is, is it possible to have a function that returns something which as far as I know means that the return value/function is on the stack and pass the result to a function which takes a reference?
With my limited knowledge I would say no, but thats just a feeling that I have, and I’ve learned to never trust my feelings when it comes to programming.
I’m not too familiar with references in PHP, but you can do this in C/C++ and some cases in C#.
The thing is that you have to be careful. A value on the stack is only valid as long as the function whos stack frame it belongs to hasn’t returned (ignoring stuff like the red zone). C and C++ expect you to be smart enough to not use a dangling reference like this.
Anyway, it doesn’t matter in PHP because it’s a high level language and hides details of allocation like that from you. Most likely, all your objects are allocated on the heap.