473,487 Members | 2,483 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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=array();
}

function addcomment($comment)
{//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=array();
}

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($comment)
{//add a children
$this->tab_comments[]=$comment;
}
}//fin classe comment

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

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

//tree
//add fathers to contener
$commentaires->addcomment($commentaire1);
$commentaires->addcomment($commentaire2);
$commentaires->addcomment($commentaire3);

//children-father relationship
$commentaire1->addcomment($commentaire4);
$commentaire1->addcomment($commentaire5);
$commentaire1->addcomment($commentaire6);

$commentaire5->addcomment($commentaire7);
$commentaire5->addcomment($commentaire8);
$commentaire5->addcomment($commentaire9);

//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

Jul 17 '05 #1
1 1825
pepete wrote:

<snip>
//tree
//add fathers to contener
$commentaires->addcomment($commentaire1);
$commentaires->addcomment($commentaire2);
$commentaires->addcomment($commentaire3);

//children-father relationship
$commentaire1->addcomment($commentaire4);
$commentaire1->addcomment($commentaire5);
$commentaire1->addcomment($commentaire6);

$commentaire5->addcomment($commentaire7);
$commentaire5->addcomment($commentaire8);
$commentaire5->addcomment($commentaire9);

//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 !!


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(&$comment) {...}

Read info about references on PHP website:
http://www.php.net/manual/en/language.references.php
http://www.php.net/manual/en/languag...ences.pass.php
Jul 17 '05 #2

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

Similar topics

2
8230
by: dasod | last post by:
I would like to know if my method to remove list objects is correct in this small test program. It seems to me that there might be a simplier way, but I'm afraid I don't know enough about list...
11
1748
by: thechaosengine | last post by:
Hi all, I have a very general but quite significant question about objects. My question is, when should I create them? I know thats a crap question so let me explain a bit further. Lets...
9
1863
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
6
2543
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
161
7677
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
7
8201
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
21
2188
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not...
27
2524
by: SasQ | last post by:
Hello. I wonder if literal constants are objects, or they're only "naked" values not contained in any object? I have read that literal constants may not to be allocated by the compiler. If the...
14
5974
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
7106
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.