Assigning class objects to variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cory Bosma

    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($clas sarray)
    {
    $groupout = "";

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

    return $groupout;
    }

    also even when I try the following the same problem occurs

    function LineGroup($clas sarray)
    {
    $groupout = "";

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

    return $groupout;
    }

    Any help would be greatly appreciated.

    Thanks, Cory
  • Henk Verhoeven

    #2
    Re: Assigning class objects to variables

    Hi Cory,
    [color=blue]
    > while ($line = each($classarra y))[/color]

    In general you should make loops like this:

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

    but this will work too:

    reset($classarr ay);
    while (list($key, $line) = each($classarra y))

    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.
    [color=blue]
    > $groupout .= "<tr><td>".$cla ssarray[0]->GetCat1Desc( ).[/color]
    "</td></tr>";

    I do't know, maybe there is no element with index 0? try:
    print_r($classa rray);
    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('firs t'), new SomeClass('seco nd') );

    $classarray = array('someclas s', 'someotherclass ');
    so that i can do:
    if (in_array(get_c lass($objectarr ay[0]), $classarray) )
    print "the class of object 0 is in the class array";

    just a matter of avoiding misleading names.

    Cory Bosma wrote:[color=blue]
    > 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($clas sarray)
    > {
    > $groupout = "";
    >
    > while ($line = each($classarra y))
    > {
    > $groupout .= "<tr><td>".$lin e -> GetCat1Desc()." </td></tr>";
    > }
    >
    > return $groupout;
    > }
    >
    > also even when I try the following the same problem occurs
    >
    > function LineGroup($clas sarray)
    > {
    > $groupout = "";
    >
    > $groupout .= "<tr><td>".$cla ssarray[0] ->
    > GetCat1Desc()." </td></tr>";
    >
    > return $groupout;
    > }
    >
    > Any help would be greatly appreciated.
    >
    > Thanks, Cory[/color]

    Comment

    Working...