Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

basic php help needed

Status
Not open for further replies.

spicymango

Programmer
May 25, 2008
119
CA
Hi,

I am trying to create an array of objects of class 'My' and then print that array. What am I doing wrong here, i did not get any thing in
var $name when I print array.


Code:
<?php

class My 
{ 
        var $name = ""; 
        var $children = array(); 
        

        function My($name) 
        { 
                $this->$name = $name; 
               
	} 

        function addChild($mynode) 
        { 
                $this->children[] = $mynode; 
                
        } 
} 



        $a = new My("John"); 
        $b = new My("Bob"); 
        $c = new My("Cat"); 
        $x = new My("Xon"); 

        $a->addChild($b); 
        $a->addChild($c); 
        $a->addChild($x); 

       print_r($a->children);
	 
?>


output

Array
(
[0] => node Object ( [name] => [children] => Array ( ) => B )
[1] => node Object ( [name] => [children] => Array ( ) [C] => C )
[2] => node Object ( [name] => [children] => Array ( ) [X] => X )

)
 
your output looks correct to me.

your class is slightly wrong however. the My method should look like this

Code:
$this->[red][s]$[/s][/red]name = $name;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top