recursivity with objects

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

    recursivity with objects

    Hi,

    I am trying to do a script that is must print a tree of object (my
    objects are some comments).

    For more details, a "comment" is an object which can be at the top of
    the tree (father), or which can be an answer to another comment
    (children). There is no limit in father-children relation (a father can
    have children, which can have children, which ...).

    I have made 2 object, the comment and a comment contener (just an
    array). Un comment is simply composed with a texte and an id.

    Here is the code and my test failure.

    I want to display the tree (commen id):
    1->4
    ->5->7->8->9
    ->6
    2->
    3->

    Explain : commen 1 has 3 answers (4-5-6), 2 & 3 have not answer. Comment
    5 has one answer (7) which has one answer(8) ...

    /*------------------------------------------*/
    <?php
    classe comments
    {//contener

    //comments array
    var $tab_comments;

    function comments()
    {//constructor
    $this->tab_comments=a rray();
    }

    function addcomment($com ment)
    {//add a comment into the array
    $this->tab_comments[]=$comment;
    }
    }//end comments

    class comment
    {//the comment
    var $id;
    var $contenu;

    //children array
    var $tab_comments;

    function comment($num, $texte)
    {//constructor
    $this->id=$num;
    $this->contenu=$texte ;
    $this->tab_comments=a rray();
    }

    function outprint()
    {
    echo "I am comment number {$this->id}<br>";
    $this->printFils();
    }

    function printFils()
    { //print all children
    foreach ($this->tab_comments as $comment)
    {
    $comment->outprint();
    }
    }

    function addcomment($com ment)
    {//add a children
    $this->tab_comments[]=$comment;
    }
    }//fin classe comment

    //Here is the test
    //contener
    $commentaires=n ew comments();

    //comments
    $commentaire1=n ew comment('1','co mmentaire 1');
    $commentaire2=n ew comment('2','co mmentaire 2');
    $commentaire3=n ew comment('3','co mmentaire 3');
    $commentaire4=n ew comment('4','co mmentaire 4');
    $commentaire5=n ew comment('5','co mmentaire 5');
    $commentaire6=n ew comment('6','co mmentaire 6');
    $commentaire7=n ew comment('7','co mmentaire 7');
    $commentaire8=n ew comment('8','co mmentaire 8');
    $commentaire9=n ew comment('9','co mmentaire 9');

    //tree
    //add fathers to contener
    $commentaires->addcomment($co mmentaire1);
    $commentaires->addcomment($co mmentaire2);
    $commentaires->addcomment($co mmentaire3);

    //children-father relationship
    $commentaire1->addcomment($co mmentaire4);
    $commentaire1->addcomment($co mmentaire5);
    $commentaire1->addcomment($co mmentaire6);

    $commentaire5->addcomment($co mmentaire7);
    $commentaire5->addcomment($co mmentaire8);
    $commentaire5->addcomment($co mmentaire9);

    //Display $commentaire1
    $commentaire1->outprint();

    /* end test */
    ?>

    The result is :
    I am comment number 1
    I am comment number 4
    I am comment number 5
    I am comment number 6


    This mean that the tree is not completly read and that I need help !!

    Best regards

    Pepete

  • Zurab Davitiani

    #2
    Re: recursivity with objects

    pepete wrote:

    <snip>
    [color=blue]
    > //tree
    > //add fathers to contener
    > $commentaires->addcomment($co mmentaire1);
    > $commentaires->addcomment($co mmentaire2);
    > $commentaires->addcomment($co mmentaire3);
    >
    > //children-father relationship
    > $commentaire1->addcomment($co mmentaire4);
    > $commentaire1->addcomment($co mmentaire5);
    > $commentaire1->addcomment($co mmentaire6);
    >
    > $commentaire5->addcomment($co mmentaire7);
    > $commentaire5->addcomment($co mmentaire8);
    > $commentaire5->addcomment($co mmentaire9);
    >
    > //Display $commentaire1
    > $commentaire1->outprint();
    >
    > /* end test */
    > ?>
    >
    > The result is :
    > I am comment number 1
    > I am comment number 4
    > I am comment number 5
    > I am comment number 6
    >
    >
    > This mean that the tree is not completly read and that I need help !![/color]

    If you are using PHP4, this is because PHP4 passes values, not references by
    default. You can fix this by modifying the the addcomment method definition
    with a reference argument.

    So, the way you have it now, when you add $commentaire5 to $commentaire1, a
    copy of $commentaire5 is made. Any further changes to $commentaire5 are
    made to the original variable, and not to the copy held by $commentaire1.

    Your updated function definition should look like:

    function addcomment(&$co mment) {...}

    Read info about references on PHP website:


    Comment

    Working...