PHP object references and iterating through an array

This is a discussion on PHP object references and iterating through an array within the PHP Language forums, part of the PHP Programming Forums category; I want to store objects in ana array and then iterate through the array, retrieving a (reference) to each object ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-04-2008
Ronald Raygun
 
Posts: n/a
Default PHP object references and iterating through an array

I want to store objects in ana array and then iterate through the array,
retrieving a (reference) to each object in the array, and calling a
method on the array. I am not sure how to do it, but this is pseudocode
of what I want to do:

<?php
$m_fieldItems = createObjects();
$outstr = '';

for($i=0; $i < count($m_fieldItems); $i++)
$outstr .= $m_fieldItems[$i]->Method1()

return $outstr;
?>

My main questions about the code is this:

1). to the object (good), or am I inadvertendly causing a copy of the
object being retrieved to be created (bad and clumsy). If I am causing a
copy of the object at the ith position to be created, how do I enforce
that I return the object by REFERENCE rather than by VALUE?

Are there any other 'gotchas' I need to be aware of?
Reply With Quote
  #2 (permalink)  
Old 05-04-2008
Piotr
 
Posts: n/a
Default Re: PHP object references and iterating through an array

Ronald Raygun wrote:
> I want to store objects in ana array and then iterate through the array,
> retrieving a (reference) to each object in the array, and calling a
> method on the array. I am not sure how to do it, but this is pseudocode
> of what I want to do:
>
> <?php
> $m_fieldItems = createObjects();
> $outstr = '';
>
> for($i=0; $i < count($m_fieldItems); $i++)
> $outstr .= $m_fieldItems[$i]->Method1()
>
> return $outstr;
> ?>
>
> My main questions about the code is this:
>
> 1). to the object (good), or am I inadvertendly causing a copy of the
> object being retrieved to be created (bad and clumsy). If I am causing a
> copy of the object at the ith position to be created, how do I enforce
> that I return the object by REFERENCE rather than by VALUE?
>
> Are there any other 'gotchas' I need to be aware of?


In PHP5 (please don't use PHP4..) objects are passed by reference
*by default*. So your code in this regard is ok.

Few other things:
1. you miss a semicolon after Method1()
2. you should check for object type before you call Method1(), array
field could be something different then the object you expect.

best regards
Piotr N
Reply With Quote
  #3 (permalink)  
Old 05-04-2008
Ronald Raygun
 
Posts: n/a
Default Re: PHP object references and iterating through an array


> In PHP5 (please don't use PHP4..) objects are passed by reference
> *by default*. So your code in this regard is ok.
>
> Few other things:
> 1. you miss a semicolon after Method1()
> 2. you should check for object type before you call Method1(), array
> field could be something different then the object you expect.
>
> best regards
> Piotr N


Thanks for the clarification Piotr
Reply With Quote
  #4 (permalink)  
Old 05-04-2008
Rik Wasmus
 
Posts: n/a
Default Re: PHP object references and iterating through an array

On Sun, 04 May 2008 10:47:51 +0200, Ronald Raygun <invalid@domain.com>
wrote:

> I want to store objects in ana array and then iterate through the array,
> retrieving a (reference) to each object in the array, and calling a
> method on the array. I am not sure how to do it, but this is pseudocode
> of what I want to do:
>
> <?php
> $m_fieldItems = createObjects();
> $outstr = '';
>
> for($i=0; $i < count($m_fieldItems); $i++)
> $outstr .= $m_fieldItems[$i]->Method1()
>
> return $outstr;
> ?>
>
> My main questions about the code is this:
>
> 1). to the object (good), or am I inadvertendly causing a copy of the
> object being retrieved to be created (bad and clumsy). If I am causinga
> copy of the object at the ith position to be created, how do I enforce
> that I return the object by REFERENCE rather than by VALUE?
>
> Are there any other 'gotchas' I need to be aware of?


Aside from the answer you allready got: A foreach loop would be handier:

foreach($m_fieldItems as $item) $outstr .= $item->Method1();

In this case, indeed from PHP5 on a reference to the object will be used,
so nu duplication. If you have non-objects in an array, you can also avoid
duplication by &:

foreach($m_fieldItems as &$item) $outstr .= $item->Method1();
--
Rik Wasmus
Reply With Quote
  #5 (permalink)  
Old 05-05-2008
Gordon
 
Posts: n/a
Default Re: PHP object references and iterating through an array

On May 4, 9:47*am, Ronald Raygun <inva...@domain.com> wrote:
> I want to store objects in ana array and then iterate through the array,
> retrieving a (reference) to each object in the array, and calling a
> method on the array. I am not sure how to do it, but this is pseudocode
> of what I want to do:
>
> <?php
> * * $m_fieldItems = createObjects();
> * * $outstr = '';
>
> * * for($i=0; $i < count($m_fieldItems); $i++)
> * * * $outstr .= $m_fieldItems[$i]->Method1()
>
> * * return $outstr;
> ?>
>
> My main questions about the code is this:
>
> 1). to the object (good), or am I inadvertendly causing a copy of the
> object being retrieved to be created (bad and clumsy). If I am causing a
> copy of the object at the ith position to be created, how do I enforce
> that I return the object by REFERENCE rather than by VALUE?
>
> Are there any other 'gotchas' I need to be aware of?


Are you using PHP 4? If so then the problem will be in the function
that builds your array of objects in the first place. In PHP 4 the =
operator causes an object copy to be created. This catches a lot of
guys who have OO experience out as it's not what happens in most other
OO languages that I'm aware of.

One approach is to use =& to build your array instead of =, ad =&
causes a reference to be created and assigned instead of the object to
be copied.

But if you're doing any kind of meaningful OO work then by far the
better option is to upgrade to PHP 5. The object model in 5 is far
more complete than the one in 4, with public, protected and private
members, class constants, static members, uniform constructors and
destructors, exceptions (if you like that sort of thing :) ) and, most
importantly, objects are copied by reference by default.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:03 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0