Brasem Posted March 8, 2009 Share Posted March 8, 2009 Hello everyone, I was experimenting a bit with php today and i was trying to find out if it is possible to create an object, give it an object attribute and access this objects attributes. I try this in the operation display(). $this->var1->var2 generates an error (var1 is an object of class b). The constructing of var 1 goes well since it echoes what i wrote in the construct function. But $this->var1->var2 does not work. What am i doing wrong in this experiment? Thanks for your help Bram ------------CODE-------------- <html> <head> </head> <body> <?php class a { public $var1; public function display() { $this->var1 = new b(10); //var1 is object echo "trying to access attribute \$var2 of object \$var1 $this->var1->var2"; } } class b { public $var2; public function __construct($c) { $this->var2=$c; echo "inside class b \$var2 has value $this->var2"; } } $test = new a(); $test->display(); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/148520-object-having-an-object-attribute-beginner/ Share on other sites More sharing options...
premiso Posted March 8, 2009 Share Posted March 8, 2009 Class b needs to extend class a. Also you should really put the class definitions in a separate file and include it. Quote Link to comment https://forums.phpfreaks.com/topic/148520-object-having-an-object-attribute-beginner/#findComment-779902 Share on other sites More sharing options...
Brasem Posted March 8, 2009 Author Share Posted March 8, 2009 Class b needs to extend class a. Also you should really put the class definitions in a separate file and include it. Thanks for the tip, I know about putting the class definitions in another file. I didn't know i needed to extend in this case too. I changed 'class b' into 'class b extends a'. It still gives the same error message though Catchable fatal error: Object of class b could not be converted to string in /var/www/test/test.php on line 13 line 13 corresponds to echo "trying to access attribute \$var2 of object \$var1 $this->var1->var2"; Quote Link to comment https://forums.phpfreaks.com/topic/148520-object-having-an-object-attribute-beginner/#findComment-779909 Share on other sites More sharing options...
Mchl Posted March 8, 2009 Share Posted March 8, 2009 Class b needs to extend class a. Nope. Just do echo "trying to access attribute \$var2 of object \$var1 {$this->var1->var2}"; Quote Link to comment https://forums.phpfreaks.com/topic/148520-object-having-an-object-attribute-beginner/#findComment-779912 Share on other sites More sharing options...
Brasem Posted March 8, 2009 Author Share Posted March 8, 2009 Class b needs to extend class a. Nope. Just do echo "trying to access attribute \$var2 of object \$var1 {$this->var1->var2}"; Thanks a lot ! that did it Quote Link to comment https://forums.phpfreaks.com/topic/148520-object-having-an-object-attribute-beginner/#findComment-779914 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.