468,296 Members | 1,322 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Assigning class objects to variables

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
1 1549
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.

Similar topics

13 posts views Thread by Bryan Parkoff | last post: by
17 posts views Thread by Calle Pettersson | last post: by
1 post views Thread by matthew.macdonald-wallace | last post: by
8 posts views Thread by =?Utf-8?B?VHJlY2l1cw==?= | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.