470,848 Members | 1,685 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

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?
Jun 2 '08 #1
4 1596
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
Jun 2 '08 #2
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
Jun 2 '08 #3
On Sun, 04 May 2008 10:47:51 +0200, Ronald Raygun <in*****@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
Jun 2 '08 #4
On May 4, 9:47*am, Ronald Raygun <inva...@domain.comwrote:
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.
Jun 2 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Steven Post | last post: by
3 posts views Thread by Ali Tahbaz | last post: by
4 posts views Thread by Fabrizio | last post: by
3 posts views Thread by Jack Addington | last post: by
5 posts views Thread by Michael Moreno | last post: by
6 posts views Thread by Jake Barnes | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.