473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1682
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steven Post | last post by:
Hi all, I've got a situation where I have a given string and I need to find which option in a select object has a value matching this string. I know I could do it by iterating through all of the...
3
by: Ali Tahbaz | last post by:
I'm having trouble iterating through LinkSources in an Excel workbook using C#. I first wrote the below code in VBA to get a quick, correct result, Dim x As Variant For Each x In...
4
by: Fabrizio | last post by:
Hi I cannot figure why it isn't possible to cast a struct array to an object array. I written a structure like this: public struct Test { private int TestA; private int TestB;
3
by: Jack Addington | last post by:
Quite new to C# but I am getting quite confused with Array's and ArrayLists. This is probably the same old iteration of a basic question but I can't seem to find a clear answer/example of this...
6
by: Gary Frank | last post by:
What are the ramifications if I were to instantiate an object tens of thousands of times and add them to an array? Or hundreds of thousands of times? Do you know if the act of instantiating a...
5
by: Varangian | last post by:
ImageButton ship; ship = new ImageButton; for (int i=0; i<5; i++) { ship.ImageUrl = pathofImage; ship.ID = "ShipNo" + i.ToString(); ship.Click += new...
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
6
by: Jake Barnes | last post by:
I was just reading this article on Ajaxian: http://ajaxian.com/archives/show-love-to-the-object-literal This is a newbie question, but what is the object literal? I thought it was like an...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.