473,378 Members | 1,449 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,378 software developers and data experts.

PHP's References

I'm trying to create a stack of class objects; but on cycling through them
with 'foreach' I am unable to update their properties. What is the correct
method for this?
Jul 16 '05 #1
3 1653
ba***@blues.no wrote:
I'm trying to create a stack of class objects; but on cycling through them
with 'foreach' I am unable to update their properties. What is the correct
method for this?


You get copies with foreach; use the key value to access the
original array:

foreach ($foo as $bar->$baz)
{
$foo[$bar] = new MyClass($baz->Name, $baz->WidgetCount);
}

HTH

You really could have thought of this one yourself and tried the code, you
know!

Jul 16 '05 #2
ba***@blues.no schrieb:
I'm trying to create a stack of class objects; but on cycling through them
with 'foreach' I am unable to update their properties. What is the correct
method for this?


foreach() uses a copy of the array. Use somthing like:

<?php
foreach($arr as $key=>$val) {
$arr[$key] = editfunc($val);
}
?>

or for very large arrays

<?php
reset($arr);
while(list($key,$val) = each($arr)) {
$arr[$key] = editfunc($val);
}
?>

Regards
Thomas

Jul 16 '05 #3
Thanks to both Thomas and matty; I actually was unaware of the second form
of 'foreach'. So ended up doing something like the following (which even
keeps me from having to use references.) I'd like to ask one more thing; is
the call to array_keys, neglible in effeciency compared to using the second
form of 'foreach'?

class Foo
{
var $some_value;

function Bar()
{
$this->some_value = "def";
}

function Foo()
{
$this->some_value = "abc";
}
}

$class_stack = array();
array_push( $class_stack, new Foo() );
array_push( $class_stack, new Foo() );

// $some_value is set to "abc"

foreach( $class_stack as $cls=>$key )
{
$class_stack[ $cls ]->Bar();
}

// $some_value now equals "def"
Jul 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Alexander Baranovsky | last post by:
Let me to introduce myself. I'm author of the paxScript scripting engine: www.paxscript.com. My recent invention is the LISPPA technology (List Processing based on the Polymorphic Arrays): ...
2
by: Rob Long | last post by:
Hi, I've got this really annoying problem, which I think might be a bug, but I wanted to check that I'm not missing some subtle PHP feature. I've created a user-defined list type - a bit like...
7
by: MRe | last post by:
Hi, I'm new to PHP programming (coming from C++); and all is well, except when it comes to memory management. Particularly, with passing objects/arrays in and out of functions. I've had so many...
2
by: Robert Misiorowski | last post by:
Hello, Everything started with me wanting to write a little command line php script that uses some gd functions. After a little research, I realized I need to unccomment the line in my php.ini...
6
by: ittay.dror | last post by:
Hi, I'm a java developer trying to get acquainted with PHP. In java web development, the most popular web applications framework is Struts, which is equivalent to Smarty. The problem with...
11
by: Anze | last post by:
Hi! I have a script that uses a lot of memory and an ISP that has memory-limit set to 10 Mb. The script of course doesn't finish running. :( The program is rather simple - it parses XML into...
40
by: Shufen | last post by:
Hi all, Can someone who has use PHP before and know quite well about the language, tell me what are the stuffs that Python offers and PHP doesn't. A few examples will be nice. I know about the...
15
by: Chung Leong | last post by:
Here's a little brain teaser distilled from a bug that took me a rather long time to figure out. The two functions in the example below behave differently. The difference is easy to spot, of...
18
by: bb nicole | last post by:
Below is my php code which i need to save the jobseeker resume in database. But does not function and show the message: Column count doesn't match value count at row 1 after i add a field name...
1
by: Taras_96 | last post by:
Hi all As I understand it, object assignment causes the variable to contain the same object identifier, whereas assigning a variable via reference creates a true alias of the RHS: <?php ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.