hno Posted February 16, 2009 Share Posted February 16, 2009 HI i'm is learning about using calss in php.I wrote this code: class Person { function __construct($name) { $this->name = $name; } function getName() { return $this->name; } private $name; }; $a= new Person("a") . "\n"; print $a->getName(); When I using this code ,it gives me the following error: "Catchable fatal error: Object of class Person could not be converted to string in C:\wamp\www\class\class.php on line 14" but when I change it to following ,it doen't have any errors and show me the result. class Person { function set($name) { $this->name = $name; } function getName() { return $this->name; } private $name; }; $a = new Person; $a->set("a"); print $a->getName(); What is the problem? thanks Quote Link to comment https://forums.phpfreaks.com/topic/145371-question-about-class/ Share on other sites More sharing options...
genericnumber1 Posted February 16, 2009 Share Posted February 16, 2009 The error is in this line: <?php $a = new Person("a") . "\n"; print $a->getName(); You probably meant to do... <?php $a= new Person("a"); print $a->getName() . "\n"; which should work fine. Quote Link to comment https://forums.phpfreaks.com/topic/145371-question-about-class/#findComment-763169 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.