By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,045 Members | 1,139 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,045 IT Pros & Developers. It's quick & easy.

Assigning class objects to variables

P: n/a
Looked all over for the answer to this but can't find it. Maybe I'm
searching for the wrong thing. I'm new to using classes in PHP.

Whenever I try and reference the member's ($line) properties or
functions I either get blank for properties or "Call to a member
function on a non-object" for functions. I'm sure I'm doing something
incorrectly with the assignment.

function LineGroup($classarray)
{
$groupout = "";

while ($line = each($classarray))
{
$groupout .= "<tr><td>".$line -> GetCat1Desc()."</td></tr>";
}

return $groupout;
}

also even when I try the following the same problem occurs

function LineGroup($classarray)
{
$groupout = "";

$groupout .= "<tr><td>".$classarray[0] ->
GetCat1Desc()."</td></tr>";

return $groupout;
}

Any help would be greatly appreciated.

Thanks, Cory
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi Cory,
while ($line = each($classarray))
In general you should make loops like this:

reset($classarray);
while (list($key) = each($classarray)) {
$line =& $classarray[$key];

but this will work too:

reset($classarray);
while (list($key, $line) = each($classarray))

but has the disadvantage of copying each object in $classarray when it
is assigned to $line. Copying decreases performance and uses up memory
that is only freed at the end of your script.
$groupout .= "<tr><td>".$classarray[0]->GetCat1Desc(). "</td></tr>";

I do't know, maybe there is no element with index 0? try:
print_r($classarray);
then select "view source" in your browser, so that you can see what's in
the array under what keys.

Greetings,

Henk Verhoeven,
www.phpPeanuts.org.

BTW, i would expect:
$objectarray = array( new SomeClass('first'), new SomeClass('second') );

$classarray = array('someclass', 'someotherclass');
so that i can do:
if (in_array(get_class($objectarray[0]), $classarray) )
print "the class of object 0 is in the class array";

just a matter of avoiding misleading names.

Cory Bosma wrote: Looked all over for the answer to this but can't find it. Maybe I'm
searching for the wrong thing. I'm new to using classes in PHP.

Whenever I try and reference the member's ($line) properties or
functions I either get blank for properties or "Call to a member
function on a non-object" for functions. I'm sure I'm doing something
incorrectly with the assignment.

function LineGroup($classarray)
{
$groupout = "";

while ($line = each($classarray))
{
$groupout .= "<tr><td>".$line -> GetCat1Desc()."</td></tr>";
}

return $groupout;
}

also even when I try the following the same problem occurs

function LineGroup($classarray)
{
$groupout = "";

$groupout .= "<tr><td>".$classarray[0] ->
GetCat1Desc()."</td></tr>";

return $groupout;
}

Any help would be greatly appreciated.

Thanks, Cory


Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.