RE: [PHP] References
| From: | Vidyut Luther | Date: | Tue, 07 Nov 2000 21:20:44 +0000 |
| Subject: | RE: [PHP] References | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-24195@lists.php.net to get a copy of this message | ||
On Tue, 7 Nov 2000, Neil Kimber wrote:
> There's nothing to stop you returning result directly, so:
>
> $ref = &$result;
> return $ref;
>
> becomes
>
> return $result;
I knew that, but my question was why can't I just return a reference.. I
know that my code is basically sending $result back. But I guess this is
where my lack of comp sci is killing me, I thought a reference could be
thought of as a messenger to the program "saying hey u want $result, it's
over there".. basically eliminating the need for the messenger to carry
the message, just point to where the message is. or in more technical
terms, I thought using references saved time and memory because you're
not sending the entire blob of data in memory over to the function that
called another function. You're just sending a pointer to the contents of
the variable.
>
> With regard to references in functions, this is a matter of scope. A parser
> (or compiler) will create memory space for any variables local to a
> function. However, this memory space only exists for the duration of the
> function. Once the function terminates the parser releases the memory
> meaning that any references to it are no longer valid. This allocation of
> memory occurs on what is called the Heap. Consider the Heap to be a huge
> block of unused memory available for the computer to use at it pleases. Thus
> you cannot return the memory address of a variable. However, you can alter
> the contents of variables within another function by passing them by
> reference to the function.
but if I return the reference to a variable, i usually do it at the end of
the function and the function has ended. From what you're saying the
memorry block should be empty now, since the function has been exited. But
it doesn't seem to be the case.. or I'm not understanding you.
> If you pass variables to a function as a parameter then it is store on the
> Stack. Each call to a function gets it's own stack space (think about the
> stack growing upwards like a .... stack). When a parser trys to resolve a
> variable name it will look in an internal lookup table that will direct it
> to the Heap or the stack. When looking for items on the stack the parser
> starts at the top and then works it's way down until it reaches the first
> variable that matches the name. The stack allows the same variable name to
> be used in each function section of the stack. Basically the rule of thumb
> for resolution of variables is first the Heap and then the first occurence
> on the stack.
>
> Errm, I'm going a bit off topic now. I'd suggest looking for a good
> reference that discusses Scope, the Call Stack and Heap allocation. Enjoy...
>
yea time to go school for real :).
> Neil
>
>
> -----Original Message-----
> From: Vidyut Luther [mailto:vluther@phpcult.com]
> Sent: 07 November 2000 19:44
> To: php-general@lists.php.net
> Subject: [PHP] References
>
>
> I was wondering why this is not possible.
>
> function get_a_lot_of_data() {
>
> $sql = "select * from hugetable";
> $result = mysql_query($sql);
> return &result;
>
> }
>
>
> I have to do this all the time,
>
> $ref = &$result;
> return $ref;
>
> why do i have to create the reference first, and then pass it ? i have no
> formal education in programming.. so any of you computer sci majors wanna
> clear this up for me ?
>
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
>